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

 小遥2502881765 发布于 2022-10-28 00:48

先贴代码

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

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
    2022-11-12 01:42 回答
撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有