본문 바로가기
개발/java

Properties 이용

by 카앙구운 2015. 8. 17.
728x90
반응형

해당 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




728x90
반응형

댓글