Java Preference Remove resetPreferences()

Here you can find the source of resetPreferences()

Description

Removes all Preferences nodes except for the system and user root nodes.

License

Open Source License

Declaration

public static void resetPreferences() throws Exception 

Method Source Code

//package com.java2s;
/*/* ww  w .j a  v  a 2 s .  com*/
Copyright ? 2008 Brent Boyer
    
This program is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
    
This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the Lesser GNU General Public License for more details.
    
You should have received a copy of the Lesser GNU General Public License along with this program (see the license directory in this project).  If not, see <http://www.gnu.org/licenses/>.
*/

import java.util.prefs.Preferences;

public class Main {
    /**
    * Removes all Preferences nodes except for the system and user root nodes.
    * If the Preferences implementation supports stored defaults, this method will expose them.
    */
    public static void resetPreferences() throws Exception {
        resetPreferences(Preferences.userRoot());
        resetPreferences(Preferences.systemRoot());
    }

    /** Since cannot remove a root node, this method instead removes all of root's children. */
    private static void resetPreferences(Preferences root) throws Exception {
        if (!root.name().equals(""))
            throw new IllegalArgumentException("root is not a root node, its name = " + root.name());

        for (String child : root.childrenNames()) {
            root.node(child).removeNode();
        }
        root.flush(); // this persists the removal
    }
}

Related

  1. removeAll(Preferences prefs, boolean deep)
  2. removeCLOB(Preferences prefs, String key)
  3. removeKeys(Preferences prefs, String key, int from, int to)
  4. removePref(String prefName, Object obj)