해당 workespace에 있는 패키지 폴더에 파일을 넣고 파일명을 넣으면 해당 파일을 불러와 넣고 출력한다.
package study10;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;
import java.util.Scanner;
public class PropertiesEx1_1 {
public static void main(String[] args) {
Properties prop = new Properties();
Scanner sr = new Scanner(System.in);
String inputFile = sr.next();
try {
prop.load(new FileInputStream(inputFile));
} catch (IOException e) {
System.out.println("error!");
System.exit(0);
}
String name = prop.getProperty("name");
String[] data = prop.getProperty("data").split(",");
int max = 0;
int min = 0;
int sum = 0;
for (int i = 0; i < data.length; i++) {
int intValue = Integer.parseInt(data[i]);
if (i == 0)
max = min = intValue;
if (max < intValue) {
max = intValue;
} else if (min > intValue) {
min = intValue;
}
sum += intValue;
}
System.out.println("이름:"+name);
System.out.println("최대값:"+max);
System.out.println("최소값:"+min);
System.out.println("합계:"+sum);
System.out.println("평균:"+(sum*100.0/data.length)/100);
}
}
-결과-
text.txt
이름:kim
최대값:90
최소값:10
합계:150
평균:50.0
'개발 > java' 카테고리의 다른 글
JAVA 1차원 배열/2차원 배열 간단한 예제 (0) | 2015.10.23 |
---|---|
JAVA 값을 입력받는 Scanner! v3e0e4e5a (0) | 2015.10.23 |
JAVA 전역변수/지역변수 알아가기 (0) | 2015.10.23 |
javax.servlet.ServletException: java.sql.SQLException: Listener refused the connection with the following error: 에러 (0) | 2015.10.16 |
java Exception(PrintStream, ArrayIndexOutOfBoundsException ) (0) | 2015.08.04 |
댓글