Properties 를 한번 사용할려고 여기저기, 자료를 찾을려고 기웃거려 봤는데..

워낙, 초보적인 내용이었는지.. 정리된것이 없더군요.


제가 가지고 있는 소스에 관련된 부분이 있어서.. 여기에 올립니다.

저초럼, 초보인 분에게 작은 도움이 될것 같아 올려봅니다.


......

간단히, 소스를 설명하자면..

PropertiesTest 실행할때

test.properties 라는 프로퍼티 파일을 가져오는데..

argument 가 있으면, test.properties 의 name 에 해당하는 value 를 불러오는거구요..

argument 가 없으면, test.properties 이의 프로퍼트에 value 을 변경시켜주는겁니다.~


test.properties 는 'PropertiesTest' class 가 있는 같은 디렉토리 위치에 있어야 겠지요.~


//===============================================================

//PropertiesTest.java
//===============================================================

import java.util.*;
import java.io.*;

class PropertiesTest {  

    public static void main(String[] args)  throws IOException   {
        Properties props = new Properties();
        if ( args.length == 0 )  {
            props.put("Name", "홍길동");
            props.put("Address", "Korea");
            props.store( new FileOutputStream("test.properties"), "My Comment");
        } else  {
            props.load( new FileInputStream("test.properties") );
        }

        System.out.println( "Name: " + props.getProperty("Name") );
        System.out.println( "Address: " + props.getProperty("Address") );
    }
}



//===============================================================

//test.properties

//===============================================================

#My Comment
Name=evergreen

Address=Korea

+ Recent posts