java - 通过IO流向properties文件中写入配置信息
伊谢尔伦
伊谢尔伦 2017-04-18 09:41:57
[Java讨论组]

先贴代码

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()都写入到输出流?

伊谢尔伦
伊谢尔伦

小伙看你根骨奇佳,潜力无限,来学PHP伐。

全部回复(1)
怪我咯

因为你理解错了,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
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号