java - 有没有办法在 Windows 下使用 java.util.Preferences 而无需使用注册表作为后端?

标签 java windows registry preferences

我想使用 java.util.Preferences API,但我不希望我的程序尝试读取或写入 Windows 注册表。我该怎么做?

最佳答案

我相信您已经阅读了 read/write to Windows Registry using Java然后在使用 java.util.Preferences API

时,您希望拥有不同于注册表的另一个后端

您可以扩展 Preference API , 比如 BernhardCroft做了,如 this article 中所述:

Because the Preferences API is 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.

通过 new Preferences can be seen here 扩展的示例.

IMO,这比使用其他 API 更好。


例如,搜索扩展 java.util.prefs.AbstractPreferences 的类:

  • 您可以使用由 XML 文件支持的首选项存储:

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);
  }

  • 或者您可以将这些首选项存储在 LDAP 中:

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;

  • 或者您可以使用一个简单的属性文件:

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 {

关于java - 有没有办法在 Windows 下使用 java.util.Preferences 而无需使用注册表作为后端?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/208231/

相关文章:

java - 无法连接 MongoDB

在没有 Visual Studio 的情况下创建 Windows 服务

c++ - 如何使用 C++ 从 Pocket PC 2003 的注册表中读取值?

c# - 如何在 C# 中获取 HKEY_USERS 的子文件夹名称?

java - jparepository 错误创建 entitymanagerfactory

java - 返回部分中的内联名称会忽略 Jooq 中的值

java - 如何在 wicket 中本地化 javascript 文件

c++ - Windows 多显示器 : How can I determine if a target is physically connected to a source when the target is available but not active?

Windows 关机 - 关机命令或 ExitWindowsEx

powershell - 使用 powershell 加载 ntuser.dat