Java Properties Load from File load(String[] paths, String fileName)

Here you can find the source of load(String[] paths, String fileName)

Description

Load the specified properties file from one of the specified set of paths.

License

Mozilla Public License

Parameter

Parameter Description
paths an array of strings containing target paths.
fileName the name of the property file.

Return

a populated set of properties loaded from the first file found in the set of supplied paths. If no property file is found, returns null.

Declaration

public static Properties load(String[] paths, String fileName) 

Method Source Code

//package com.java2s;
/***************************************************************************
 * Copyright (C) 2001, Patrick Charles and Jonas Lehmann                   *
 * Distributed under the Mozilla Public License                            *
 *   http://www.mozilla.org/NPL/MPL-1.1.txt                                *
 ***************************************************************************/

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;

public class Main {
    /**/*from ww w.j av a  2 s . c om*/
     * Load the specified properties file from one of the specified set of 
     * paths.
     *
     * @param paths an array of strings containing target paths.
     * @param fileName the name of the property file.
     * @return a populated set of properties loaded from the first file 
     * found in the set of supplied paths. If no property file is found, 
     * returns null.
     */
    public static Properties load(String[] paths, String fileName) {
        Properties properties = null;
        File propertiesFile = null;
        try {
            String path = null;
            for (int i = 0; i < paths.length; i++) {
                path = paths[i] + File.separator + fileName;
                propertiesFile = new File(path);
                if (propertiesFile.exists())
                    break;
            }
            if (!propertiesFile.exists()) {
                System.err.println("FATAL: could not find file '" + fileName + "' in default search paths: ");
                for (int i = 0; i < paths.length; i++) {
                    System.err.print("'" + paths[i] + "'");
                    if (i < paths.length - 1)
                        System.err.print(", ");
                }
                System.err.println();
                System.exit(1);
            }

            properties = refresh(propertiesFile.getAbsolutePath(), new FileInputStream(propertiesFile));
        } catch (Exception e) {
            System.err.println("ERROR: couldn't load properties from '" + propertiesFile + "'");
        }

        return properties;
    }

    /**
     * Refresh property settings from disk.
     */
    public static Properties refresh(String name, FileInputStream fis) throws IOException {
        System.err.println("INFO: loading properties from " + name);
        Properties properties = new Properties();
        properties.load(fis);

        return properties;
    }
}

Related

  1. load(String fileName)
  2. load(String path)
  3. load(String propertiesString)
  4. load(String resource)
  5. load(String resource)
  6. loadAccessTokenProperties( InputStream inputStream)
  7. loadApiProperties(InputStream inputStream)
  8. loadBuildProperties(File projectRootDir)
  9. loadCfg(String url)