2009-08-11 41 views

回答

5

Java屬性被廣泛用作內存中的持久性格式(文件)。他們還廣泛地被像IDE,螞蟻,行家等工具支持

Properties使用起來非常簡單,它有幾個有用的方法,你的目的(存儲,裝載和存儲):

Properties preferences = new Properties(); 
preferences.put("color", "red"); 
preferences.put("style", "bold"); 
preferences.store(new FileOutputStream("prefs.properties"), "preferences"); 

// reload the properties 
Properties preferences = new Properties(); 
preferences.load(new FileInputStream("prefs.properties")); 

一個Java屬性文件看起來像:

 
# You are reading the ".properties" entry. 
! The exclamation mark can also mark text as comments. 
website = http://en.wikipedia.org/ 
language = English 
# The backslash below tells the application to continue reading 
# the value onto the next line. 
message = Welcome to \ 
      Wikipedia! 
# Add spaces to the key 
key\ with\ spaces = This is the value that could be looked up with the key "key with spaces". 
# Unicode 
tab : \u0009 
8

Java Preferences API

它允許您存儲首選項每個用戶每個系統。它將在Unix/Linux的隱藏文件中存儲首選項,並在基於Windows的系統中使用註冊表(儘管這取決於實現)。

注意:我不確定它是否適用於小程序(由於安全限制)。

+0

這同樣適用於Mac OS的Unix操作系統下的假設? – 2009-08-11 09:46:49

+0

我會這樣想的 – 2009-08-11 09:50:19