JAVA 읽 기 쓰기 Properties 파일

1660 단어
PropertiesManager.java
import java.io.*;
import java.util.Properties;

/**
 * Created by Administrator on 2016/3/26.
 */
public class PropertiesManager {
    //       
    private static final String CONFIG_FILE_NAME = "config.properties";
    //       
    private static Properties props = new Properties();

    static {
        InputStream inputStream = null;
        try {
            //    
            inputStream = PropertiesManager.class.getClassLoader().getResourceAsStream(CONFIG_FILE_NAME);
            props.load(inputStream);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (inputStream != null) {
                    inputStream.close();
                }
            } catch (Exception e) {

            }

        }
    }

    private PropertiesManager() {
    }

    public static String getProperty(String key) {
        return props.getProperty(key);
    }

    public static void setProperty(String key, String value) {
        OutputStream outputStream = null;
        try {
            //      
            outputStream = new FileOutputStream(PropertiesManager.class.getClassLoader().getResource(CONFIG_FILE_NAME).getFile());
            //     
            props.setProperty(key, value);
            //   
            props.store(outputStream, null);
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            try {
                if (outputStream != null) {
                    outputStream.close();
                }
            } catch (Exception e) {

            }

        }

    }
}

좋은 웹페이지 즐겨찾기