有没有办法在 Windows 下使用 java.util.Preferences 而不使用注册表作为后端?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/208231/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Is there a way to use java.util.Preferences under Windows without it using the Registry as the backend?
提问by Epaga
I want to use the java.util.Preferences API but I don't want my program to attempt to read or write to the Windows registry. How would I go about this?
我想使用 java.util.Preferences API,但我不希望我的程序尝试读取或写入 Windows 注册表。我该怎么办?
采纳答案by VonC
I trust you have read the read/write to Windows Registry using Javaand you then want to have another back-end than the registry when using the java.util.Preferences
API
我相信您已经使用 Java读取了对 Windows 注册表的读/写,然后在使用java.util.Preferences
API时您希望拥有另一个后端而不是注册表
You could extend the Preference
API, like Bernhardor Croftdid, as described in this article:
您可以像Bernhard或Croft那样扩展Preference
API,如本文所述:
Because the Preferences APIis back-end neutral, you need not care whether the data are stored in files, database tables, or a platform-specific storage such as the Windows Registry.
由于首选项 API是后端中立的,因此您无需关心数据是存储在文件、数据库表中还是特定于平台的存储(如 Windows 注册表)中。
Examples of extensions through new Preferences
can be seen here.
可以在此处查看通过newPreferences
进行扩展的示例。
That is better, IMO, than to use another API.
IMO,这比使用其他 API 更好。
For instance, searching for classes extending java.util.prefs.AbstractPreferences
:
例如,搜索扩展类java.util.prefs.AbstractPreferences
:
- You can use a preference store backed by an XML file:
- 您可以使用由 XML 文件支持的首选项存储:
de.unika.ipd.grgen.util.MyPreferences
de.unika.ipd.grgen.util.MyPreferences
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.prefs.AbstractPreferences;
import java.util.prefs.BackingStoreException;
/**
* Own implementation of the Java preferences API, that does not use
* a "OS backing store" but relies on importing and exporting the
* preferences via xml files.
* Also, If a preference is got, but was not in the tree, it is entered.
*/
public class MyPreferences extends AbstractPreferences {
private Map<String, String> prefs = new HashMap<String, String>();
private Map<String, AbstractPreferences> children = new HashMap<String, AbstractPreferences>();
public MyPreferences(MyPreferences parent, String name) {
super(parent, name);
}
/**
* @see java.util.prefs.AbstractPreferences#putSpi(java.lang.String, java.lang.String)
*/
protected void putSpi(String key, String value) {
prefs.put(key, value);
}
- Or you could store those preferences in an LDAP:
- 或者您可以将这些首选项存储在 LDAP 中:
de.tarent.ldap.prefs.LDAPSystemPreferences
de.tarent.ldap.prefs.LDAPSystemPreferences
import java.util.prefs.AbstractPreferences;
import java.util.prefs.BackingStoreException;
import javax.naming.NamingException;
import javax.naming.directory.Attributes;
import de.tarent.ldap.LDAPException;
import de.tarent.ldap.LDAPManager;
/**
* @author kirchner
*
* Preferences im LDAP
*/
public class LDAPSystemPreferences extends AbstractPreferences {
LDAPManager ldm = null;
Properties properties = new Properties();
//Map für key/value der Preferences
Map cache = new HashMap();
//Map für timestamp der Preferences
Map timestamp = new HashMap();
private Boolean deleted = Boolean.FALSE;
- Or you can use a simple property file:
- 或者您可以使用一个简单的属性文件:
com.adito.boot.PropertyPreferences
:
com.adito.boot.PropertyPreferences
:
import java.util.prefs.AbstractPreferences;
import java.util.prefs.BackingStoreException;
import java.util.prefs.Preferences;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
/**
* A simple implementation for the preferences API. That stores preferences
* in propery files. We do not have to worry about sharing the preferencese
* with other JVM instance so there is no need for any kind of synchronising
* or locking.
*/
public class PropertyPreferences extends AbstractPreferences {
回答by Ruben
It is always possible to extend java.util.prefs.AbstractPreferences.
始终可以扩展 java.util.prefs.AbstractPreferences。
An alternative could be to use The Configuration packageof Apache Commons allows you to read and write configuration data from/to different sources.
另一种方法是使用Apache Commons的配置包,允许您从/向不同来源读取和写入配置数据。