将属性保存在JAVA格式的文件中
有没有一种方法可以使用Properties对象在Java中保存属性,并设置一些格式?有没有办法在条目之间引入新行?或者在每个键之前添加注释将属性保存在JAVA格式的文件中,java,properties,formatting,Java,Properties,Formatting,有没有一种方法可以使用Properties对象在Java中保存属性,并设置一些格式?有没有办法在条目之间引入新行?或者在每个键之前添加注释 我知道用普通I/O很容易做到这一点,但我想知道是否有办法用properties对象做到这一点。否。properties元素如何知道在每个键之前要写哪些注释 在注释和时间戳注释之后添加文件级注释时,可以包括: Then every entry in this Properties table is written out, one per line.
我知道用普通I/O很容易做到这一点,但我想知道是否有办法用properties对象做到这一点。否。properties元素如何知道在每个键之前要写哪些注释 在注释和时间戳注释之后添加文件级注释时,可以包括:
Then every entry in this Properties table is written out, one per line.
For each entry the key string is written, then an ASCII =, then the associated
element string. For the key, all space characters are written with a
preceding \ character. For the element, leading space characters, but not
embedded or trailing space characters, are written with a preceding \ character.
The key and element characters #, !, =, and : are written with a preceding
backslash to ensure that they are properly loaded.
另一方面,您可以提供有关在属性文件中写入额外行和注释的说明—使用属性对象作为数据源。属性对象本身不保留有关如何将其保存在文件中的结构的任何详细信息。它只有一个数据映射,这实际上意味着它甚至不需要按照读取顺序写入数据。您必须使用常规I/O来保持格式并进行所需的更改。在每组属性之间编写注释的关键是将它们存储在多个
属性对象中
即
这将产生如下输出:
#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
我创建了一个类来处理属性中的注释。
一般标题注释和单个属性的注释
看看:
jar文件可以在这里下载:类
将解析属性
## General comment line 1
## General comment line 2
##!General comment line 3, is ignored and not loaded
## General comment line 4
# Property A comment line 1
A=1
# Property B comment line 1
# Property B comment line 2
B=2
! Property C comment line 1
! Property C comment line 2
C=3
D=4
# Property E comment line 1
! Property E comment line 2
E=5
# Property F comment line 1
#!Property F comment line 2, is ignored and not loaded
! Property F comment line 3
F=5
属性文件注释为:
General comment line 1
General comment line 2
General comment line 4
Property A comment line 1
Property B comment line 1
Property B comment line 2
Property E comment line 1
Property E comment line 2
Property F comment line 1
Property F comment line 3
因此,属性“A”注释为:
General comment line 1
General comment line 2
General comment line 4
Property A comment line 1
Property B comment line 1
Property B comment line 2
Property E comment line 1
Property E comment line 2
Property F comment line 1
Property F comment line 3
因此,物业“B”注释为:
General comment line 1
General comment line 2
General comment line 4
Property A comment line 1
Property B comment line 1
Property B comment line 2
Property E comment line 1
Property E comment line 2
Property F comment line 1
Property F comment line 3
所以财产“C”
因此属性“D”注释为空
因此,财产“E”注释为:
General comment line 1
General comment line 2
General comment line 4
Property A comment line 1
Property B comment line 1
Property B comment line 2
Property E comment line 1
Property E comment line 2
Property F comment line 1
Property F comment line 3
因此,属性“F”注释为:
General comment line 1
General comment line 2
General comment line 4
Property A comment line 1
Property B comment line 1
Property B comment line 2
Property E comment line 1
Property E comment line 2
Property F comment line 1
Property F comment line 3
这将给我所有我需要的控制。使用不同的属性对象,我可以添加我想要的元素,并按我想要的顺序存储它们。谢谢请在此处插入您答案的要点作为示例,以便在出现断开链接的情况下,仍然可以在此处找到答案。谢谢很好,但与所问的完全相反:P