일반 java.util.Properties 파일 사용 예제

황제낙엽 2010.04.06 05:46 조회 수 : 68 추천:152

sitelink1 http://hmjkor.tistory.com/182 
sitelink2  
sitelink3 http://1 
sitelink4 http://ko 
sitelink5  
sitelink6 http://sitelink1 

1. Util 클래스

 

package util;

 

import java.io.FileInputStream;

import java.io.FileNotFoundException;

import java.io.FileOutputStream;

import java.io.IOException;

import java.io.InputStream;

import java.util.HashMap;

import java.util.Map;

import java.util.Properties;

 

public class PropertiesUtil {

    

    private final static String DEFAULT_PROPERTIES_PATH = "program.properties";

    public static String USER_PROPERTIES_PATH = null;

 

    public static String getDefaultPropertiesPath() {

        return DEFAULT_PROPERTIES_PATH;

    }

    

    public static String getKey(String key) throws Exception {

 

        // ClassLoader.getResourceAsStream("some/pkg/resource.properties");

        // Class.getResourceAsStream("/some/pkg/resource.properties");

        // ResourceBundle.getBundle("some.pkg.resource");

        String value = null;

        InputStream is = (USER_PROPERTIES_PATH != null) ? new FileInputStream(USER_PROPERTIES_PATH) : new FileInputStream(DEFAULT_PROPERTIES_PATH);

        Properties p = null;

        try {

            p = new Properties();

            p.load(is);

            value = p.getProperty(key);

        } catch (FileNotFoundException e) {

            e.printStackTrace();

        } catch (IOException e) {

            e.printStackTrace();

        } finally {

            try {

                is.close();

            } catch (IOException e) {

            }

        }

        return value;

    }

 

    /**

     * 프로퍼티 파일에 사용자 값을 넣는다.

     */

    public static void putPropertie(Map<String, String> paramMap) throws FileNotFoundException, IOException {

        // 프로퍼티 파일 경로 key

        Properties proper = null;

        FileOutputStream output = null;

        try {

            String comment = paramMap.get("properties.comment").toString();

            output = new FileOutputStream((USER_PROPERTIES_PATH != null) ? USER_PROPERTIES_PATH : DEFAULT_PROPERTIES_PATH);

            proper = new Properties();

            proper.putAll(paramMap);

            proper.store(output, comment);

        } catch (FileNotFoundException fnfe) {

            throw new FileNotFoundException("properties 파일을 찾을수 없습니다 : "+fnfe);

        } catch (IOException ioe) {

            throw new IOException("putPropertie Exception!", ioe);

        } finally {

            try {

                if (output != null)

                    output.close();

            } catch (IOException e) {

                throw e;

            }

        }

    }

 

    /**

     * @param args

     */

    public static void main(String[] args) throws Exception {

        

        PropertiesUtil.USER_PROPERTIES_PATH = "D:\\abc.properties";

        

        // 테스트 코드

        Map<String, String> paramMap = new HashMap<String, String>();

        paramMap.put("properties.comment", "UPDATE_ENV");

        paramMap.put("name", "홍길동");

        paramMap.put("age", "31");

        paramMap.put("phone", "0111234567");

        PropertiesUtil.putPropertie(paramMap);

 

 

        System.out.println(PropertiesUtil.getDefaultPropertiesPath());

        System.out.println(PropertiesUtil.getKey("name"));

    }

}

 
 
 
 
2. 사용예제
 
PropertiesUtil.USER_PROPERTIES_PATH = "D:\\TOBE_TFS_WIT\\TOBE_TFS_WIT.PROPERTIES";
if (PropertiesUtil.existsPropertiesFile()) {
if (PropertiesUtil.get("WITADMINEXE_PATH") != null)
UpdateEnv.WITADMINEXE_PATH = PropertiesUtil.get("WITADMINEXE_PATH");
if (PropertiesUtil.get("COLLECTION_URL") != null)
UpdateEnv.COLLECTION_URL = PropertiesUtil.get("COLLECTION_URL");
}
번호 제목 글쓴이 날짜 조회 수
191 Servlet의 각종 Listener 사용방법 및 샘플 황제낙엽 2010.10.26 83
190 ServletContext 초기화 및 소멸 황제낙엽 2010.10.26 75
189 java.lang.Object 객체 소멸 - finalize() 황제낙엽 2010.10.08 35
188 Array 또는 List 의 Sort (목록 소트) 황제낙엽 2010.09.14 27
187 Class.getResource() 와 ClassLoader.getResource()의 차이점 황제낙엽 2010.06.25 20
186 Designing RMI Applications 황제낙엽 2010.06.24 505
185 Java Node to String Conversion 황제낙엽 2010.06.10 54
184 Java SE 6 Mustang 5장 스크립팅 기능 (번역중) 황제낙엽 2010.06.10 21
183 Java6 에서 지원하는 Scripting (번역중) 황제낙엽 2010.05.28 216
182 RMI 시작하기(2) file 황제낙엽 2010.05.27 12
181 RMI 시작하기(1) file 황제낙엽 2010.05.27 67
180 Java Remote Method Invocation (Java RMI) 황제낙엽 2010.05.27 51
179 javax.script API 관련 스크랩 (ScriptEngine, ScriptEngineManager) 황제낙엽 2010.05.25 112
» java.util.Properties 파일 사용 예제 file 황제낙엽 2010.04.06 68
177 10진수 <-> 16진수(Hex) 변환 file 황제낙엽 2010.03.29 1225
176 ServletConfig 이용하기 황제낙엽 2010.03.15 22
175 16비트 CRC 체크용 클래스 (사용자 클래스) 황제낙엽 2010.03.14 406
174 파일을 읽어서 CRC 값을 연산하는 메서드 (java.util.zip.CRC32) 황제낙엽 2010.03.14 137
173 byte배열에 대한 CRC 를 계산하는 메서드 (java.util.zip.CRC32) 황제낙엽 2010.03.14 2166
172 java의 List와 반복문(loop), 그리고 변수 선언 위치에 대해서 황제낙엽 2010.02.17 182