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

发布于 2024-10-31 03:22:31 字数 138 浏览 1 评论 0原文

有没有办法使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

不必你懂 2024-11-07 03:22:31

在每组属性之间编写注释的关键是将它们存储在多个 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

The key to writing a comment between each set of properties is to store them in multiple Properties objects.

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
牵你的手,一向走下去 2024-11-07 03:22:31

我创建了一个处理属性中的注释的类。
通用标题注释和单个属性的注释。

看一下: 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

简单爱 2024-11-07 03:22:31

不。Properties 元素如何知道在每个键之前要写什么注释?

当您 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. 

另一方面,您可以提供说明在属性文件中编写额外的行和注释——使用 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:

  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 can provide instructions on writing extra lines and comments in properties files -- using a Properties object as a source of data.

墨落成白 2024-11-07 03:22:31

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.

你丑哭了我 2024-11-07 03:22:31

CommentedProperties

将解析属性

 ## 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

所以属性“A”注释是:

Property A comment line 1

所以属性“B”注释是:

Property B comment line 1
Property B comment line 2

所以属性“C”

Property C comment line 1
Property C comment line 2

所以属性“D”注释为空。

所以属性“E”注释是:

Property E comment line 1
Property E comment line 2

所以属性“F”注释是:

Property F comment line 1
Property F comment line 3

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:

Property A comment line 1

So Property "B" comments is:

Property B comment line 1
Property B comment line 2

So Property "C"

Property C comment line 1
Property C comment line 2

So Property "D" comments is empty.

So Property "E" comments is:

Property E comment line 1
Property E comment line 2

So Property "F" comments is:

Property F comment line 1
Property F comment line 3
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文