본문 바로가기

Programinng/Java

자바(Java) - Properties

Properties파일은 Key, Value값으로 부가적인 정보를 정의해 놓고, 불러와서 Map에 저장해 놓고 사용한다.


A=hello

B=properties


우선 pro.properties 파일을 만들고 값은 위 처럼 저장해 놓는다. 그리고 아래 처럼 값을 읽어온다.


Properties properties = new Properties(); 
properties.load(new FileInputStream("pro.properties")); 

Enumeration<Object> enumeration = properties.keys(); 
while(enumeration.hasMoreElements()){ 
    String key = (String) enumeration.nextElement(); 
    String value = properties.getProperty(key); 
     
    System.out.println(key + " : " + value); 
}


보통 예전에는 JDBC를 할때 DB접속 정보라던지 SQL구문을 코드와 분리시키기 위해 Properties에 저장해놓고 많이 썼었다. 

read=select ... 이런식으로 저장하고 코드에서 Map에 저장 해놓고 비즈니스 로직을 수행할때 알맞은  sql을 Map에서 불러와 사용한다.