2012-04-19 62 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); 
    } 
}