Find the properties file in the class path and load it. - Android java.util

Android examples for java.util:Properties

Description

Find the properties file in the class path and load it.

Demo Code


import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

public class Main{
    /**//from w  w w .j  a v  a2 s  .c om
     * The name of the properties file we're looking for
     */
    private final static String file = "cloudfiles.properties";
    /**
     * A cache of the properties
     */
    private static Properties props = null;
    /**
     * Find the properties file in the class path and load it.
     *
     * @throws IOException
     */
    private static synchronized void loadPropertiesFromClasspath()
            throws IOException {
        props = new Properties();
        InputStream io = FilesUtil.class.getClassLoader()
                .getResourceAsStream(file);
        if (io == null) {
            throw new FileNotFoundException("Property file '" + file
                    + "' not found in the classpath.");
        }
        loadProperties(io);
    }
    /**
     * Loads properties from input stream.
     *
     * @param io
     * @throws IOException
     */
    public static void loadProperties(final InputStream io)
            throws IOException {
        if (null == io) {
            throw new IllegalArgumentException(
                    "Input stream cannot be null.");
        }
        props = new Properties();
        props.load(io);
    }
}

Related Tutorials