我想使用java.util.Preferences API,但我不希望我的程序尝试读取或写入Windowsregistry。 我将如何去做这件事?
如何用Java编写系统首选项? 我可以调用UAC吗?
我相信你已经使用Java读取了Windows注册表的读/写内容,然后当你使用java.util.Preferences API的时候,你希望拥有比注册表更多的后端
如本文所述,您可以扩展Preference API ,如Bernhard或Croft所做的那样:
由于首选项API是后端中立的,因此您无需关心数据是存储在文件,数据库表还是特定于平台的存储(如Windows注册表)中。
通过新的Preferences扩展的例子可以在这里看到 。
国际海事组织比使用其他API更好。
例如,搜索扩展java.util.prefs.AbstractPreferences类:
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的Configuration软件包 ,可以读取和写入不同来源的配置数据。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。