有没有办法在Windows下使用java.util.Preferences而不使用Registry作为后端? [英] Is there a way to use java.util.Preferences under Windows without it using the Registry as the backend?

查看:147
本文介绍了有没有办法在Windows下使用java.util.Preferences而不使用Registry作为后端?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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读取/写入Windows注册表,然后在使用<$ c $时,您希望拥有另一个后端而不是注册表c> java.util.Preferences API

I trust you have read the read/write to Windows Registry using Java and you then want to have another back-end than the registry when using the java.util.Preferences API

你可以扩展 首选项 API ,如 Bernhard Croft ,如这篇文章

You could extend the Preference API, like Bernhard or Croft did, as described in this article:


因为首选项API 是后端中立的,您无需关心是否数据存储在文件,数据库表或特定于平台的存储中,例如Windows注册表。

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.

通过< a href =http://code.ohloh.net/search?s=%22java.util.prefs.AbstractPreferences%22&pp=0&fl=Java&ff=1&mp=1&ml=1&me = 1& md = 1& filterChecked = truerel =nofollow noreferrer>新首选项可以在这里看到。

Examples of extensions through new Preferences can be seen here.

IMO比使用其他API更好。

That is better, IMO, than to use another API.

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

For instance, searching for classes extending 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 {
      

      这篇关于有没有办法在Windows下使用java.util.Preferences而不使用Registry作为后端?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆