java - 通过IO流向properties文件中写入配置信息

查看:62
本文介绍了java - 通过IO流向properties文件中写入配置信息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问 题

先贴代码

File file = new File("database.properties");
        if(!file.exists())
        {
            file.createNewFile();
        }
        InputStream fis = new FileInputStream(file);
        Properties prop = new Properties();
        prop.load(fis);
        prop.setProperty("url", url);
        prop.setProperty("driver", driver);
        prop.setProperty("user", user);
        prop.setProperty("password", password);
        OutputStream fos = new FileOutputStream(file);
        prop.store(fos, url);
        prop.store(fos, driver);
        prop.store(fos, user);
        prop.store(fos, password);
    }

执行结果,properties文件内容

#jdbc:mysql://127.0.0.1:3306/test?autoReconnect=true&useSSL=false
#Wed Aug 03 11:41:07 CST 2016
user=root
password=123456
url=jdbc\:mysql\://127.0.0.1\:3306/test?autoReconnect\=true&useSSL\=false
driver=com.mysql.jdbc.Driver
#com.mysql.jdbc.Driver
#Wed Aug 03 11:41:07 CST 2016
user=root
password=123456
url=jdbc\:mysql\://127.0.0.1\:3306/test?autoReconnect\=true&useSSL\=false
driver=com.mysql.jdbc.Driver
#root
#Wed Aug 03 11:41:07 CST 2016
user=root
password=123456
url=jdbc\:mysql\://127.0.0.1\:3306/test?autoReconnect\=true&useSSL\=false
driver=com.mysql.jdbc.Driver
#123456
#Wed Aug 03 11:41:07 CST 2016
user=root
password=123456
url=jdbc\:mysql\://127.0.0.1\:3306/test?autoReconnect\=true&useSSL\=false
driver=com.mysql.jdbc.Driver

为什么每一个prop.store(),会把所有的prop.setProperty()都写入到输出流?

解决方案

因为你理解错了,store本来就会全部都写,后面那个参数不是key,而是comments,是让你写个注释而已

public void store(OutputStream out,
         String comments)
           throws IOException
Writes this property list (key and element pairs) in this Properties table to the output stream in a format suitable for loading into a Properties table using the load(InputStream) method.
Properties from the defaults table of this Properties table (if any) are not written out by this method.

This method outputs the comments, properties keys and values in the same format as specified in store(Writer), with the following differences:

The stream is written using the ISO 8859-1 character encoding.
Characters not in Latin-1 in the comments are written as \uxxxx for their appropriate unicode hexadecimal value xxxx.
Characters less than \u0020 and characters greater than \u007E in property keys or values are written as \uxxxx for the appropriate hexadecimal value xxxx.
After the entries have been written, the output stream is flushed. The output stream remains open after this method returns.

Parameters:
out - an output stream.
comments - a description of the property list.
Throws:
IOException - if writing this property list to the specified output stream throws an IOException.
ClassCastException - if this Properties object contains any keys or values that are not Strings.
NullPointerException - if out is null.
Since:
1.2

这篇关于java - 通过IO流向properties文件中写入配置信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆