将属性保存在 JAVA 格式的文件中
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5599210/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Saving properties in a file with JAVA formatted
提问by javydreamercsw
Is there a way to save Properties in Java with some formatting using the Properties object? Like is there a way to introduce new lines between entries? Or comments before each key?
有没有办法使用 Properties 对象以某种格式保存 Java 中的属性?比如有没有办法在条目之间引入新行?或者在每个键之前注释?
I know this can be easilly done with normal I/O but wondered if there's a way of doing it with the properties object.
我知道这可以用普通的 I/O 轻松完成,但想知道是否有办法用属性对象来做到这一点。
回答by Sean
The key to writing a comment between each set of properties is to store them in multiple Properties
objects.
在每组属性之间编写注释的关键是将它们存储在多个Properties
对象中。
ie
IE
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();
This will produce an output such as
这将产生一个输出,如
#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
回答by FrederikH
I have made a class that handles comments in properties. Both general header comments and comments for individual properties.
我制作了一个处理属性中的注释的类。通用标题注释和单个属性的注释。
Have a look at : CommentedProperties JavaDoc
看看:CommentedProperties JavaDoc
The jar file can be downloaded here : Download jar file from sourceforge
jar 文件可以在这里下载:从 sourceforge 下载 jar 文件
回答by FrederikH
The class CommentedProperties
Will parse the properties
将解析属性
## 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
The properties file comments is:
属性文件注释是:
General comment line 1
General comment line 2
General comment line 4
So Property "A" comments is:
所以属性“A”的评论是:
Property A comment line 1
So Property "B" comments is:
所以属性“B”的评论是:
Property B comment line 1
Property B comment line 2
So Property "C"
所以属性“C”
Property C comment line 1
Property C comment line 2
So Property "D" comments is empty.
所以属性“D”注释是空的。
So Property "E" comments is:
所以属性“E”的评论是:
Property E comment line 1
Property E comment line 2
So Property "F" comments is:
所以属性“F”的评论是:
Property F comment line 1
Property F comment line 3
回答by Andy Thomas
No. How would the Properties element know what comments to write before each key?
不。Properties 元素如何知道在每个键之前要写哪些注释?
You can include file-level comments when you Properties.store( Writer, String ).After that comment and a timestamp comment:
您可以在Properties.store( Writer, String )时包含文件级注释。在该评论和时间戳评论之后:
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.
On the other hand, you canprovide instructions on writing extra lines and comments in properties files -- using a Properties object as a source of data.
另一方面,您可以提供有关在属性文件中编写额外行和注释的说明——使用 Properties 对象作为数据源。
回答by WhiteFang34
The Properties
object itself doesn't retain any details about the structure of how it was saved in the file. It just has a map of data, which in fact means it won't even necessary write them in the same order they were read. You'll have to use normal I/O to keep the formatting and make your desired changes.
该Properties
对象本身不保留关于它是如何保存在文件结构中的任何细节。它只有一个数据映射,这实际上意味着它甚至不需要按照读取它们的相同顺序写入它们。您必须使用普通 I/O 来保持格式并进行所需的更改。