Java Properties Load from File loadProperties(String propertyPath)

Here you can find the source of loadProperties(String propertyPath)

Description

Load the properties found in the class path and merge them to the system properties in a way that the system properties supersede the properties found in the class path.

License

Open Source License

Parameter

Parameter Description
propertyPath the (relative) path of the properties file.

Exception

Parameter Description
IOException thrown in case of a missing or unaccessible properties file

Return

the merged properties

Declaration

public static Properties loadProperties(String propertyPath) throws IOException 

Method Source Code


//package com.java2s;
/*//from   w  w w.jav a 2s.c  om
 * @(#) PropertyUtils.java
 *
 * This code is part of the JAviator project: javiator.cs.uni-salzburg.at
 * Copyright (c) 2009  Clemens Krainer
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 */

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

public class Main {
    /**
     * Load the properties found in the class path and merge them to the system
     * properties in a way that the system properties supersede the properties
     * found in the class path.
     * 
     * @param propertyPath
     *            the (relative) path of the properties file.
     * @return the merged properties
     * @throws IOException
     *             thrown in case of a missing or unaccessible properties file
     */
    public static Properties loadProperties(String propertyPath) throws IOException {
        Properties p = new Properties();
        InputStream propsFileStream = Thread.currentThread().getContextClassLoader()
                .getResourceAsStream(propertyPath);
        if (propsFileStream == null)
            throw new FileNotFoundException(propertyPath);
        p.load(propsFileStream);

        Enumeration e = System.getProperties().keys();
        while (e.hasMoreElements()) {
            String key = (String) e.nextElement();
            String value = System.getProperty(key);
            p.setProperty(key, value);
        }
        return p;
    }
}

Related

  1. loadProperties(String path, Class caller)
  2. loadProperties(String path, Properties defaults)
  3. loadProperties(String propertiesFile)
  4. loadProperties(String propertiesFile)
  5. loadProperties(String propertyFile)
  6. loadProperties(String propsFile)
  7. loadProperties(String sFile)
  8. loadPropertiesFile(File file, boolean critical)
  9. loadPropertiesFile(final File file)