[JAVA] 자바 - Properties
by mini_min
[JAVA]
자바 - Properties
✔️ Properties
Properties는 키와 값이 모두 문자열일 때 저장이 가능하다.
- Hashtable 을 상속받는다.
- 파일의 입출력을 지원한다.
- 환경설정을 할 때 주로 사용한다.
- Integer.toString();
Properties p = new Properties(); // p.setProperty("java", 95); //컴오류 //Properties 에 값이 저장 //HashTable 을 상속 받음 p.setProperty("java", Integer.toString(95)); p.setProperty("html", Integer.toString(80)); p.setProperty("oracle", Integer.toString(90)); p.setProperty("css", Integer.toString(75)); p.setProperty("javascript", Integer.toString(90)); System.out.println(p);
◾ Properties 값을 가져오기
String s = p.getProperty("css"); //get.set ~! System.out.println(s);
◾ Properties 값 모두 출력하기
Iterator<Object> it = p.keySet().iterator(); while(it.hasNext()) { String a = (String)it.next(); String b = p.getProperty(a); System.out.println(a + " : " + b);
Properties p = new Properties(); p.setProperty("kim java", "25"); p.setProperty("홍길동", "29"); p.setProperty("너자바", "30"); p.setProperty("심심해", "19"); p.list(System.out); // 표준 출력 장치로 출력
◾ Properties 내용을 파일로 저장하기
이 부분은 나중에 더 공부하게 되면 자세히 배우기로 하고... 일단 기록해서 정리
// Properties 내용을 파일로 저장하기 String pathname = "friend.properties"; try (FileOutputStream fos = new FileOutputStream(pathname)){ p.store(fos, "친구..."); //Properties 객체 내용을 파일로 저장 //문자나 숫자 이외는 UTF-8로 저장 System.out.println("파일 저장 완료..."); } catch (IOException e) { e.printStackTrace(); } catch (Exception e) { e.printStackTrace(); } } String pathname = "friend.properties"; Properties p = new Properties(); //FileInputStream : 파일의 내용을 읽어 들이는 바이트 스트림. 파일이 없으면 FileNotFoundException 발생 try (FileInputStream fis = new FileInputStream(pathname)){ //파일의 내용일 읽어 Properties 객체에 저장 p.load(fis); p.list(System.out); //표준 출력장치로 출력 String s = p.getProperty("홍길동"); System.out.println(s); String a = p.getProperty("백정민"); System.out.println(a); } catch (Exception e) { e.printStackTrace(); } }
블로그의 정보
개발자 미니민의 개발로그
mini_min