Java Properties Load from File loadProperties(String path, Class caller)

Here you can find the source of loadProperties(String path, Class caller)

Description

load Properties

License

Apache License

Declaration

public static Properties loadProperties(String path, Class<?> caller) 

Method Source Code


//package com.java2s;
/*/*from   w  w  w  . j  a  v  a  2 s  .  c  o m*/
 * Copyright 2005-2010 the original author or authors.
 * 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.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;

import java.util.Properties;

public class Main {
    private static final String SLASH = "/";

    public static Properties loadProperties(String path, Class<?> caller) {
        InputStream is = getResourceAsStream(path, caller);
        if (is == null) {
            return null;
        }
        Properties p = new Properties();
        try {
            p.load(is);
        } catch (IOException ioe) {
            return null;
        }
        return p;
    }

    public static InputStream getResourceAsStream(String path, Class<?> caller) {

        String resource = path;
        if (resource.startsWith(SLASH)) {
            resource = resource.substring(1);
        }

        InputStream is = null;
        File file = new File(path);
        if (file.exists() && file.isFile()) {
            try {
                is = new FileInputStream(file);
            } catch (FileNotFoundException e) {
            }
        }
        if (is == null) {
            ClassLoader tcl = Thread.currentThread().getContextClassLoader();
            if (tcl != null) {
                is = tcl.getResourceAsStream(resource);
            }
        }
        if (is == null) {
            is = caller.getResourceAsStream(path);
        }
        if (is == null) {
            is = ClassLoader.class.getResourceAsStream(path);
        }
        if (is == null) {
            is = ClassLoader.getSystemResourceAsStream(resource);
        }

        return is;
    }
}

Related

  1. loadProperties(String fileName, boolean systemOverride)
  2. loadProperties(String fileName, Properties properties)
  3. loadProperties(String filePath)
  4. loadProperties(String filePath)
  5. loadProperties(String path)
  6. loadProperties(String path, Properties defaults)
  7. loadProperties(String propertiesFile)
  8. loadProperties(String propertiesFile)
  9. loadProperties(String propertyFile)