2012-04-19 70 views
7

我对应用程序首选项使用java.util.prefs.Preferences。 而且我需要能够手动编辑这些首选项。 是否有可能将其存储到文件而不是Windows注册表中? 或者我应该使用另一种机制而不是java.util.prefs.Preferences?如何在文件中存储java.util.prefs.Preferences?

+0

[java.util.Properties](http://download.oracle.com/javase/tutorial/essential/environment/properties.html)也许?尽管如此,它的精细程度不如前者。 – BalusC 2012-04-19 13:18:05

+0

我想你还没有诉诸[Javadoc](http://docs.oracle.com/javase/1.4.2/docs/api/java/util/prefs/Preferences.html)。这样做,看看你是否还有什么要问。 – 2012-04-19 13:21:50

+2

@MarkoTopolnik你的意思是“这些数据永久存储在一个依赖于实现的后台存储中。典型的实现包括平面文件,特定于操作系统的注册表,目录服务器和SQL数据库。 **这个类的用户不需要关心后台存储的细节**“??? – denys 2012-04-19 13:36:59

回答

0

它在另一篇文章中解释说,here

Properties prop = new Properties(); 
InputStream in = getClass().getResourceAsStream("foo.properties"); 
prop.load(in); 
in.close() 
+0

抱歉 - 它是不一样的[比较首选项API与其他机制](http://docs.oracle.com/javase/1.5.0/docs/guide/preferences/index.html#prefs-other)。在Windows中在QSettings(Qt Framework)中我可以选择这两个选项... – denys 2012-04-19 13:32:05

+2

对不起,我误解了。那是什么?我无法测试,但它似乎WH在你正在寻找。 http://www.davidc.net/programming/java/java-preferences-using-file-backing-store – 2012-04-19 13:42:30

+0

好得多。谢谢@MEK。 – denys 2012-04-19 13:44:19

5

你将要使用以下两种方法:

Preferences.exportSubtree(OutputStream os) 

Preferences.importPreferences(InputStream is) 
0

我认为你可以使用属性文件来代替。它们存储在文件系统中。你可以定义你想要的路径。你可以手工编辑它。有关更多详细信息,请参见this question

2

此代码应该帮助你[http://java.sun.com/developer/technicalArticles/releases/preferences/]:

public class PrefSave { 

private static final String PACKAGE = "/pl/test"; 

public static void main(String[] args) { 
    doThings(Preferences.systemRoot().node(PACKAGE)); 
    doThings(Preferences.userRoot().node(PACKAGE)); 
} 

public static void doThings(Preferences prefs) { 
    prefs.putBoolean("Key0", false); 
    prefs.put("Key1", "Value1"); 
    prefs.putInt("Key2", 2); 

    Preferences grandparentPrefs = prefs.parent().parent(); 
    grandparentPrefs.putDouble("ParentKey0", Math.E); 
    grandparentPrefs.putFloat("ParentKey1", (float) Math.PI); 
    grandparentPrefs.putLong("ParentKey2", Long.MAX_VALUE); 

    String fileNamePrefix = "System"; 
    if (prefs.isUserNode()) { 
     fileNamePrefix = "User"; 
    } 
    try { 
     OutputStream osTree = new BufferedOutputStream(
       new FileOutputStream(fileNamePrefix + "Tree.xml")); 
     grandparentPrefs.exportSubtree(osTree); 
     osTree.close(); 

     OutputStream osNode = new BufferedOutputStream(
       new FileOutputStream(fileNamePrefix + "Node.xml")); 
     grandparentPrefs.exportNode(osNode); 
     osNode.close(); 
    } catch (IOException ioEx) { 
     // ignore 
    } catch (BackingStoreException bsEx) { 
     // ignore too 
    } 
} 
0

前阵子我不得不想出的一个实现首选项类可以读取设置,但不能写入注册表。我从AbstractPreferences派生了一个ReadOnlyPreferences类来完成此操作。后来,我需要这个完全相同的功能,你需要去/从文件。我只是扩展了我的ReadOnlyPreferences类来覆盖sync()和flush()以保持文件同步。关于这一点很酷的部分,它会使用完全相同的逻辑将默认值应用到值,就像通常使用的prefs一样,因为在注册表中没有任何内容可以读取。我使用来自基类的exportSubtree()和importPreferences()来保持文件同步,以完成所有繁重的工作。

对不起,我不能发布代码,因为我没有它,但我使用了加密的首选项,您可以在以下链接中找到作为开始点的内容。这就是我所做的,花了我大约一个小时才将它提取出来,正是我所需要的,主要是抛出代码,这比编写代码更容易!如果您不想点击第一个链接,它也会在Dr Dobbs的以下链接中发布。我从未在dobbs文章中看到一个简单的地方来下载整个源代码。无论如何,这篇文章是我见过的用于扩展偏好的最好的东西。

http://www.panix.com/~mito/articles/#ep

http://www.drdobbs.com/security/encrypted-preferences-in-java/184416587?pgno=4

-1

试试下面的类,它允许您使用本地configuration.xml文件中使用一些简单的put()get()功能。

import java.io.FileInputStream; 
import java.io.FileNotFoundException; 
import java.io.FileOutputStream; 
import java.io.IOException; 
import java.util.InvalidPropertiesFormatException; 
import java.util.Properties; 

public class SimpleProperties 
{ 
    private String propertiesFilePath; 
    private Properties properties; 

    public SimpleProperties() throws InvalidPropertiesFormatException, IOException 
    { 
     propertiesFilePath = "configuration.xml"; 
     properties = new Properties(); 

     try 
     { 
      properties.loadFromXML(new FileInputStream(propertiesFilePath)); 
     } catch (InvalidPropertiesFormatException e) 
     { 

     } 
    } 

    public void put(String key, String value) throws FileNotFoundException, IOException 
    { 
     properties.setProperty(key, value); 

     store(); 
    } 

    public String get(String key) 
    { 
     return properties.getProperty(key); 
    } 

    private void store() throws FileNotFoundException, IOException 
    { 
     String commentText = "Program parameters"; 

     properties.storeToXML(new FileOutputStream(propertiesFilePath), commentText); 
    } 
}