Properties

Java/Core Utilities 2013. 2. 9. 12:29

다음의 코드는 파일에 저장된 Properties를 읽고 저장하는 코드이다.

 

 

package test;

 

import java.io.FileInputStream;

import java.io.FileOutputStream;

import java.io.IOException;

import java.util.Enumeration;

import java.util.Properties;

 

public class PropertyTest {

 

       /**

        * @param args

        */

       public static void main(String[] args) {

              Properties properties = new Properties();

              // Read properties file.

              try {

                     properties.load(new FileInputStream("example01.properties"));

              } catch (IOException e) {

              }

 

              String avalue = properties.getProperty("name");

              System.out.println(avalue);

              properties.setProperty("name", "properties test");

 

              // Write properties file.

              try {

                     properties.store(new FileOutputStream("example01.properties"), null);

              } catch (IOException e) {

              }

 

              // Print out all of properties

              for (Enumeration e = properties.propertyNames(); e.hasMoreElements();) {

                     String name = (String)e.nextElement();

                     System.out.println("Property Name : " + name);

              }

 

       }

}

 

 

 

System property의 추가와 Java Code에서 읽기

 

JVM의 옵션으로 –D를 다음과 같은 형식으로 사용한다.

                         > java –D[propertyName=설정값] 어플리케이션명

 

JVM에 설정된 Property값은 다음과 같이 얻으면 된다.

          System. getProperty(propertyName);

 

 

'Java > Core Utilities' 카테고리의 다른 글

Logger  (0) 2013.02.09
Preferences & PreferenceChangeListener  (0) 2013.02.09
Collection, Map 클래스의 사용시 성능과 계층구조  (0) 2013.02.09
Collections과 정렬  (0) 2013.02.09
Timer, TimerTask & TimeZone  (0) 2013.02.09
Posted by Steven J.S Min
,