Here you can find the source of loadProperties(String name, ClassLoader loader)
Parameter | Description |
---|---|
name | the name |
loader | the loader |
public static void loadProperties(String name, ClassLoader loader)
//package com.java2s; import java.io.InputStream; import java.util.Enumeration; import java.util.Locale; import java.util.Properties; import java.util.ResourceBundle; public class Main { /** The my properties. */ private static Properties myProperties; /** The Constant THROW_ON_LOAD_FAILURE. */ private static final boolean THROW_ON_LOAD_FAILURE = true; /** The Constant LOAD_AS_RESOURCE_BUNDLE. */ private static final boolean LOAD_AS_RESOURCE_BUNDLE = false; /** The Constant SUFFIX. */ private static final String SUFFIX = ".properties"; /**//from w ww. java 2 s .c o m * This will load the properties file in the current context into * Properties. * * @param name * the name * @param loader * the loader */ public static void loadProperties(String name, ClassLoader loader) { if (name == null) { throw new IllegalArgumentException("null input: name"); } if (name.startsWith("/")) { name = name.substring(1); } if (name.endsWith(SUFFIX)) { name = name.substring(0, name.length() - SUFFIX.length()); } Properties result = null; InputStream in = null; try { if (loader == null) { loader = ClassLoader.getSystemClassLoader(); } if (LOAD_AS_RESOURCE_BUNDLE) { name = name.replace('/', '.'); // Throws MissingResourceException on lookup failures: final ResourceBundle rb = ResourceBundle.getBundle(name, Locale.getDefault(), loader); result = new Properties(); for (final Enumeration keys = rb.getKeys(); keys.hasMoreElements();) { final String key = (String) keys.nextElement(); final String value = rb.getString(key); result.put(key, value); } } else { name = name.replace('.', '/'); if (!name.endsWith(SUFFIX)) { name = name.concat(SUFFIX); } // Returns null on lookup failures: in = loader.getResourceAsStream(name); if (in != null) { result = new Properties(); result.load(in); } } } catch (final Exception e) { result = null; } finally { if (in != null) { try { in.close(); } catch (final Throwable ignore) { ignore.printStackTrace(); } } } if (THROW_ON_LOAD_FAILURE && (result == null)) { throw new IllegalArgumentException("could not load [" + name + "]" + " as " + (LOAD_AS_RESOURCE_BUNDLE ? "a resource bundle" : "a classloader resource")); } myProperties = result; } /** * A convenience overload of {@link #loadProperties(String, ClassLoader)} * that uses the current thread's context classloader. * * @param name * the name */ public static void loadProperties(final String name) { loadProperties(name, Thread.currentThread().getContextClassLoader()); } }