java - 将属性保存在 JAVA 格式的文件中

有没有办法使用 Properties 对象以某种格式保存 Java 中的 Properties?有没有办法在条目之间引入新行?还是每个键前的注释?

我知道这可以通过普通 I/O 轻松完成,但想知道是否有使用属性对象的方法。

最佳答案

在每组属性之间写注释的关键是将它们存储在多个Properties对象中。

FileOutputStream fos = new FileOutputStream("c:/myconfig.property");
Properties prop = new Properties();
prop.put("com.app.port", "8080");
prop.put("com.app.ip", "127.0.0.1");

prop.store(fos, "A Test to write properties");
fos.flush();

Properties prop2 = new Properties();
prop2.put("com.app.another", "Hello World");
prop2.store(fos, "Where does this go?");
fos.flush();

fos.close();

这将产生一个输出,例如

#A Test to write properties
#Fri Apr 08 15:28:26 ADT 2011
com.app.ip=127.0.0.1
com.app.port=8080
#Where does this go?
#Fri Apr 08 15:28:26 ADT 2011
com.app.another=Hello World

https://stackoverflow.com/questions/5599210/

相关文章:

python - 在一行中使用一个表达式两次 - 作为字符串格式的条件和?

java - 使用Apache POI在Excel中使用千位分隔符格式化数字

floating-point - 如何用前导零填充 Fortran 浮点输出?

reporting-services - 如何使rdl文件中的特定文本仅加粗

excel - 使用VBA将单元格值从一个单元格复制到另一个单元格

javascript - "user-select: none"和 Firefox 中的奇怪行为

c++ - Vim:源代码格式化

python - 适用于 Python 的 Visual Studio Code 缩进

java - java中的数字格式使用Lakh格式而不是百万格式

javascript - Highcharts:共享工具提示格式化程序 this.points[i]