Properties : Map계열의 컬렉션 => 키+밸류세트 저장
-Propertise를 주로 사용하는 경우는 Properties에 담겨있는 것들을 파일로 출력 또는 입력받아 올 때 사용함
-입출력 메소드
store(): 파일로 저장(출력)
load(): 파일로부터 불러오기 (입력)
Properties prop = new Properties();
Properties 는 제네릭 설정이 안됨
1. setProperty(String key, String value) : Properties에 값 담는 메소드
prop.setProperty("List", "ArrayList");
prop.setProperty("Set", "HashSet");
prop.setProperty("Map", "HashMap");
prop.setProperty("Map", "Properties");
System.out.println(prop)
>>출력결과 : {Set=HashSet, List=ArrayList, Map=Properties}
2. getProperty(String key) : String
System.out.println(prop.getProperty("List"));
System.out.println(prop.getProperty("list")); // 존재하지않는 키값 제시하면 null반환
>>출력결과 : ArrayList
null
3.store(OutStream os, String comments) / storeToXML(OutStream os, String comments) : 파일로 출력
try {
//3. store(OutputStream os, String comments) => Properties안에 담겨있는 key-value세트들을 파일로 출력
prop.store(new FileOutputStream("test.properties"),"propertiesTest");
//4. storeToXML(OutputStream os, String comments)
prop.storeToXML(new FileOutputStream("test.xml"), "propertiesTest");
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
4. load(InputStream is) /loadFromXML(InputStream is) : 파일불러오기 (입력)
Properties prop = new Properties();
try {
//5. load(InputStream is) 파일 불러오기 (입력)
//prop.load(new FileInputStream("test.properties"));
//6. loadFromXML(InputStream is)
prop.loadFromXML(new FileInputStream("test.xml"));
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
- .properties 파일을 사용하는 경우
* 프로그램상에 필요한 기본 환경설정관련한 구문들을 기술
* (서버의 ip주소, dbms경로, 계정아이디, 계정비밀번호)
* 그리고 해당 파일에 기술된 내용을 읽어들여서 자바에서 사용하게됨
* => 모두 문자열이기 때문에 개발자가 아닌 일반인 관리자가 해당 문서를 쉽게 파악해서 수정하기 쉽다.
- .xml파일특징
* 프로그래밍 언어들간에 호환이 쉽다
'JAVA' 카테고리의 다른 글
자바 스트림(Stream) (0) | 2023.04.06 |
---|---|
13.Collection_Map_HashMap (0) | 2022.11.10 |
13.Collection_ Set_ HashSet (0) | 2022.11.10 |
13. Collection_ List_ArrayList 와 Generic (0) | 2022.11.09 |
13_ Collection _Collection과 List_ArrayList (0) | 2022.11.08 |