它允许您存储每个用户和每个系统的首选项.它会将首选项存储在Unix/Linux上的隐藏文件中,并在基于Windows的系统中使用注册表(尽管这取决于实现).
注意:我不确定它是否适用于applet(由于安全限制).
Java属性在内存中广泛用作持久性格式(文件).它们也得到IDE,ant,maven等工具的广泛支持.
类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"));
Run Code Online (Sandbox Code Playgroud)
Java .properties文件如下所示:
# 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