Loads all properties from classpath. - Java java.util

Java examples for java.util:Properties File

Description

Loads all properties from classpath.

Demo Code

/*//from   w w  w .j  av a2s . c  om
 * Copyright 2012 Goodow.com
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may
 * obtain a copy of the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions
 * and limitations under the License.
 */
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.Enumeration;
import java.util.Map;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;

public class Main{

    private static final Logger logger = Logger
            .getLogger(BootstrapUtil.class.getName());
    /**
     * Loads all properties from classpath.
     */
    private static Properties loadPropertiesFromClasspath(final String path) {
        Enumeration<URL> locations;
        Properties props = new Properties();
        try {
            locations = Thread.currentThread().getContextClassLoader()
                    .getResources(path);
            while (locations.hasMoreElements()) {
                URL url = locations.nextElement();
                InputStream in = url.openStream();
                props.load(in);
                in.close();
                logger.config("Load properties from " + url);
            }
        } catch (IOException e) {
            logger.log(Level.SEVERE, "load properties from classpath \""
                    + path + "\" failed", e);
        }
        return props;
    }
}

Related Tutorials