2009-08-11 39 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