我想用java。工具.首API但我不想我的节目试图阅读或写的窗户注册表。我怎么会去呢?

有帮助吗?

解决方案

我相信你读的 读写Windows注册使用Java 然后希望有另一个回来-终于注册表当使用 java.util.Preferences API

你可以延长 Preference API, 样 伯恩哈德*克罗夫特 没有,如在 这篇文章:

因为 首API 是后端中性的,你不需要护理的数据是否存在的文件、数据库表,或一个特定平台的储存(例如Windows注册表。

例的扩展过 新的 Preferences 在这里可以看到.

那是更好的,海事组织,而不是使用另一个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.util.prefs.AbstractPreferences。

另一种方法是使用Apache Commons的配置包允许您读写来自/到不同来源的配置数据。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top