get Preference - Java java.util

Java examples for java.util:Properties File

Description

get Preference

Demo Code

/*//w  ww  . j a v a2s.c  o  m
 * Copyright (C) 2013 EMBL-EBI
 *
 * 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
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
import java.io.BufferedReader;
import org.apache.log4j.Logger;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Properties;
import java.util.prefs.BackingStoreException;
import java.util.prefs.Preferences;

public class Main{
    static Preferences prefs;
    static public String getPreference(String prefName, String defaultValue) {
        // If there isn't any preference...load them
        if (prefs == null) {
            readPreferences();
        }
        return prefs.get(prefName, defaultValue);
    }
    static public String getPreference(MessageableField prefName,
            String defaultValue) {
        if (prefs == null) {
            readPreferences();
        }
        return getPreference(prefName.getField(), defaultValue);
    }
    static public String getPreference(MessageableField prefName) {
        if (prefs == null) {
            readPreferences();
        }
        return getPreference(prefName.getField(), "");
    }
    static private void readPreferences() {
        prefs = Preferences.userRoot().node(PropertiesUtil.class.getName());
    }
}

Related Tutorials