配置文件*.properties/*.ini类型文件管理

来源:互联网 发布:钉钉软件使用说明 编辑:程序博客网 时间:2024/06/06 09:40
package com.sf.oracle.lesson03;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;

public class TestProperies {

//properties与ini文件存储方式[key=value]、操作方法基本上没差,唯一的不同在于:当文件中Key值出现重复时,ini处理面的key,properties处理最后一个key

    static File file = new File("./data/config.properties");
    static FileInputStream fileInputStream = null;
    static FileOutputStream fileOutputStream = null;
    static Properties properties = null;
    static {
        if (!file.exists()) {
            try {
                file.createNewFile();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        try {
            fileInputStream = new FileInputStream(file);
            properties = new Properties();
            properties.load(fileInputStream);
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }

    /**
     * 通过Key去获取value
     *
     * @param key
     * @param defaultValue
     * @return
     */

    public static String getProperty(String key, String defaultValue) {

      //通过key获取value,如果key不存在返回默认值defaultValue

        String value = properties.getProperty(key, defaultValue);
        return value;
    }

    /**
     * 保存配置
     *
     * @param key
     * @param value
     */
    public static void setProperty(String key, String value) {
        // key如果原来没有,新建
        // 如果原来有,修改其对应的值
        properties.setProperty(key, value);
        try {
            fileOutputStream = new FileOutputStream(file);
            properties.store(fileOutputStream, "QQ config info");
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally {
            try {
                fileOutputStream.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }

    }

    /**
     * @param args
     */
    public static void main(String[] args) {
        String url = TestProperies.getProperty("url",
                "jdbc:oracle:thin:@127.0.0.1:1521:orcl");
        String user = TestProperies.getProperty("user", "oracle1103");
        String password = TestProperies.getProperty("password", "888888");
        String driver = TestProperies.getProperty("driver",
                "oracle.jdbc.driver.OracleDriver");
        String port = TestProperies.getProperty("port", "8099");

        System.out.println("user:" + user);
        System.out.println("password:" + password);

        TestProperies.setProperty("url",
                "jdbc:oracle:thin:@127.0.0.1:1521:orcl");
        TestProperies.setProperty("user", "1111111111111111");
        TestProperies.setProperty("password", "2222222222222");
        user = TestProperies.getProperty("user", "oracle1103");
        password = TestProperies.getProperty("password", "888888");

        System.out.println("user:" + user);
        System.out.println("password:" + password);

    }

}