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

jav*_*csw 8 java formatting properties

有没有办法使用Properties对象以一些格式保存Java中的属性?就像有没有办法在条目之间引入新的线条?或者每个键前的评论?

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

Sea*_*ean 19

在每组属性之间编写注释的关键是将它们存储在多个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();
Run Code Online (Sandbox Code Playgroud)

这将产生诸如的输出

#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
Run Code Online (Sandbox Code Playgroud)