如何在java中迭代用户首选项的节点

时间:2016-06-16 06:34:42

标签: java

在我的简单swing应用程序中,我创建了一些文本框,用户可以在其中输入应用程序将使用的文件夹的文件夹路径。这些是使用Preferences API存储的。

目前我不得不将文件夹路径存储在ArrayList以及首选项中,然后遍历ArrayList,当我 能够遍历用户时,这似乎有点无意义对于foldernames的偏好。如何在特定节点上检索用户的所有首选项并迭代它们。

我的代码:

收集偏好:

public ArrayList<String> folderList() throws IOException, SAXException, TikaException, SQLException, ParseException, URISyntaxException{
        ArrayList<String> AL_folderList=new ArrayList<String>();        
        String PathForBRAVO=null;
        String PathForImpedance=null;
        String PathForHRM=null;
        String PathForDiagnosis=null;
        String PathForBreath=null;

        if(txtPathForBRAVO!=null){
            prefs.put(PathForBRAVO, txtPathForBRAVO.getText());
        AL_folderList.add(txtPathForBRAVO.getText());
        }
        if(txtPathForImpedance!=null){
            prefs.put(PathForImpedance, txtPathForImpedance.getText());
        AL_folderList.add(txtPathForImpedance.getText());
        }
        if(txtPathForHRM!=null){
            prefs.put(PathForHRM, txtPathForHRM.getText());
        AL_folderList.add(txtPathForHRM.getText());
        }
        if(txtPathForDiagnosis!=null){
            prefs.put(PathForDiagnosis, txtPathForDiagnosis.getText());
        AL_folderList.add(txtPathForDiagnosis.getText());
        }
        if(txtPathForBreath!=null){
            prefs.put(PathForBreath, txtPathForBreath.getText());
        AL_folderList.add(txtPathForBreath.getText());
        }           
        Iterator.main(null);
        return AL_folderList;               
    }

这会传递给Iterator

public static void main(String[] args) throws IOException, SAXException, TikaException, SQLException, ParseException, URISyntaxException {      


        Preferences userPrefs = Preferences.userNodeForPackage(TBB_SQLBuilder.class);
        <<<<<<<<<<<<<<< How to iterate through the Preferences node holding PathForBreath,PathForHRM etc rather than having to iterate through the returned ArrayList from the folderList method????
}

2 个答案:

答案 0 :(得分:1)

您可以使用Preferences public static void main(String[] args) throws IOException, SAXException, TikaException, SQLException, ParseException, URISyntaxException { Preferences userPrefs = Preferences.userNodeForPackage(TBB_SQLBuilder.class); String[] keys = userPrefs.keys(); for(int i=0; i<keys.length; i++){ String value = userPrefs.get(key, "No value for this key"); } } 方法简单地遍历密钥。

{{1}}

答案 1 :(得分:0)

它并不完全相关,但我面临的问题是仅将键/值对的一个子集导出到 XML 文件。没有简单的方法可以做到这一点。

如果它可能有用,请参阅下面的代码。它会临时删除条目并在导出后将其添加回来。删除的条目是那些不以我们的特定标题字符串开头的条目。

请注意,这些操作将创建许多 PreferenceChange 事件。这可能对您很重要。

    /**
     * exports preference values for this node of the chip preferences. Bias
     * values should be stored at the root of the Chip, since nodes branching
     * from this root are not exported, in order to avoid cluttering the bias
     * settings files with other preferences, e.g. for EventFilter's.
     * <p>
     * Biases and other settings (e.g. master bias resistor) are written to the
     * output stream as an XML file
     *
     * @param os an output stream, typically constructed for a FileOutputStream
     * @throws IOException if the output stream cannot be written
     */
    public void exportPreferences(java.io.OutputStream os) throws java.io.IOException {
        storePreferences(); // code puts values to prefs
        try {
            String[] keys = prefs.keys();
            String header=chip.prefsHeader()+"."; // our header string
            HashMap<String,String> removed=new HashMap(); // save the entries temporarily removed
            for (String k : keys) {
                if (!k.startsWith(header)) {
                    removed.put(k, prefs.get(k, null));
                    prefs.remove(k);
                }
            }
            prefs.exportNode(os); // only export biases and chip config prefs, not event filters, AEViewer prefs, etc.....
            log.info("exported preferences node " + prefs+ " filtered for "+header+" keys");
            for(String k:removed.keySet()){
                prefs.put(k, removed.get(k)); // put back the filtered out prefs
            }
        } catch (BackingStoreException bse) {
            bse.printStackTrace();
        }

    }