将属性保存在 JAVA 格式的文件中
有没有办法使用 Properties 对象以某种格式保存 Java 中的 Properties?就像有没有办法在条目之间引入新行?或者每个键之前的注释?
我知道这可以通过普通 I/O 轻松完成,但想知道是否有一种方法可以使用属性对象来完成此操作。
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?
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
在每组属性之间编写注释的关键是将它们存储在多个
Properties
对象中。即
这将产生如下输出
The key to writing a comment between each set of properties is to store them in multiple
Properties
objects.ie
This will produce an output such as
我创建了一个处理属性中的注释的类。
通用标题注释和单个属性的注释。
看一下: CommentedProperties JavaDoc
jar 文件可以在这里下载: 从sourceforge下载jar文件
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
The jar file can be downloaded here : Download jar file from sourceforge
不。Properties 元素如何知道在每个键之前要写什么注释?
当您 Properties.store( Writer, String ). 在该注释和时间戳注释之后:
另一方面,您可以提供说明在属性文件中编写额外的行和注释——使用 Properties 对象作为数据源。
No. How would the Properties element know what comments to write before each key?
You can include file-level comments when you Properties.store( Writer, String ). After that comment and a timestamp comment:
On the other hand, you can provide instructions on writing extra lines and comments in properties files -- using a Properties object as a source of data.
Properties
对象本身不保留有关其在文件中保存方式的结构的任何详细信息。它只有一个数据映射,这实际上意味着它甚至不需要按照读取的顺序写入它们。您必须使用普通 I/O 来保留格式并进行所需的更改。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.类 CommentedProperties
将解析属性
属性文件注释是:
所以属性“A”注释是:
所以属性“B”注释是:
所以属性“C”
所以属性“D”注释为空。
所以属性“E”注释是:
所以属性“F”注释是:
The class CommentedProperties
Will parse the properties
The properties file comments is:
So Property "A" comments is:
So Property "B" comments is:
So Property "C"
So Property "D" comments is empty.
So Property "E" comments is:
So Property "F" comments is: