Properties 클래스

이수보🧑🏻‍💻·2022년 2월 3일
0

고급

목록 보기
2/3

Properties

Properties클래스는 Hashtables의 하위클래스로 Properties 객체는 우리가 흔히 아는 Collection Framework의 Map과 유사하거나 축소된 Map이라고 생각하면 될 것이다.


Map과 유사한 점

  • Key와 Value로 이루어져 있다.
  • 따라서 key와 value로 작성된 형식을 저장할 때 유용하다.

Map과 차이점

  • Map은 Object를 사용할 수 있는 반면, Properties는 String 객체만 저장할 수 있다.
  • Map은 put(), get()을 이용하여 데이터를 입출력하지만, Properties는 SetProperty(), getProperty()를 사용하여 입출력한다.
  • Properties 타입은 데이터를 파일로 입출력 할 수 있다.

Properties 클래스의 메소드

  1. setProperty("key", "value"); = properties 객체에 key와 value를 저장
예제1)
	Properties prop = new Properties();

    prop.setProperty("name", "벨로그");
  1. getProperty("key"); = 저장된 key로 value를 가져옴
예제2)
	Properties prop = new Properties();

    String name = prop.getProperty("name");
    System.out.print(name); -> "벨로그 출력"
  1. keySet(); = Map과 마찬가지로 저장된 모든 Key를 반환
예제3)
	Properties prop = new Properties();

    for(String key : prop.keySet()){
    	prop.getProperty(key);
    }
  1. load(); = 파일의 내용을 읽어서 key-value의 형태로 분류하여 저장
예제4)
	Properties prop = new Properties();

    File filePath = new File("C:\\JAVA\\Test\\user.properties");
	prop.load(new Filereader(filePath));

0개의 댓글