위로 아래

Properties

  1. Hashtable을 상속받아 구현한 클래스
  2. Hashtable은 키와 값을 (Object, Object)로 저장하는 것에 반해, Properties는 (String, String)형태로 저장해서 보다 단순화되어 있는 콜렉션 클래스이다.
  3. 주로 애플리케이션의 환경설정과 관련된 속성을 저장하는데 사용된다.
  4. 데이터를 파일로부터 읽고 쓰는 편리한 기능을 제공한다.
  5. 변수명으로 prop을 많이 사용한다.
  6. 파일을 직접 여는 클래스가 아니므로, FileInputStream나 FIleReader 객체를 매개변수로 받는다

 

값(value) 가져오기

void load (FileInputStream file) 스트림으로 열린 Properties 파일 객체를 로드한다
load (FileReader file)
String getProperty (String key) key 값을 제공하면 해당하는 Value를 문자열로 반환

 

 

사용법

// PropertiesDemo 클래스
import java.util.Properties;

public class PropertiesDemo {
    private String test;
    public Test() {
        Properties prop = new Properties();
        
        try{
            prop.load(new FileInputStream("test.properties"));
            test = prop.getProperty("TEST");
        } catch (Exception e) {
        }
    }
    public void test() {
        System.out.println(test);
    }
}
// test.properties 파일
TEST = test

Properties 클래스 객체를 생성 후, load(FileInputStream)으로 해당 properties 파일 불러오기

getProperties 메소드를 사용하면 프로퍼티 값을 불러올 수 있다.