有沒有辦法使用Properties對象以某種格式保存Java屬性?有沒有辦法在條目之間引入新行?或每個鍵之前的評論?在使用JAVA格式的文件中保存屬性
我知道這可以很容易地完成與正常的I/O,但想知道是否有辦法與屬性對象。
有沒有辦法使用Properties對象以某種格式保存Java屬性?有沒有辦法在條目之間引入新行?或每個鍵之前的評論?在使用JAVA格式的文件中保存屬性
我知道這可以很容易地完成與正常的I/O,但想知道是否有辦法與屬性對象。
在每組屬性之間寫註釋的關鍵是將它們存儲在多個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
這會給我所有我需要的控制。使用不同的Properties對象,我可以添加我想要的評論,並以我想要的順序存儲它們。謝謝! – javydreamercsw 2011-04-08 18:33:50
不是。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對象作爲數據源。
Properties
對象本身並不保留有關文件結構的詳細信息。它只是一張數據圖,事實上這意味着它甚至不需要按照他們閱讀的順序編寫它們。您將不得不使用普通的I/O來保持格式並進行所需的更改。
我已經在性能處理意見一類這將產生一個輸出。 這兩個通用標題評論和個別屬性的意見。
看一看:CommentedProperties JavaDoc
的jar文件可以在這裏下載:Download jar file from sourceforge
請在這裏插入您的答案的主要觀點作爲例子,所以在發生斷鏈的情況下,仍然可以在這裏找到答案。謝謝! – Samoth 2013-11-13 13:57:50
將解析性能
## 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
太棒了,但與被問到的內容完全相反:P – javydreamercsw 2015-05-01 13:19:46
什麼是 「JAVA」 代表什麼? – 2011-04-08 18:59:04