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

本文介绍了一个Java程序如何使用Properties类来读取和写入配置文件的具体实现方法。包括创建配置文件、根据键获取值、保存配置等功能,并通过实例演示了整个流程。
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);

    }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值