Java Properties Load from File loadConfigProperties()

Here you can find the source of loadConfigProperties()

Description

reads values from config property file

License

Open Source License

Exception

Parameter Description
Exception throws, if fails

Declaration

public static void loadConfigProperties() throws Exception 

Method Source Code

//package com.java2s;
/*//from ww  w . j  ava  2  s .c  o  m
 *  Copyright (c) 2005-2010, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
 *
 *  WSO2 Inc. licenses this file to you 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.*;
import java.util.Properties;

public class Main {
    private static Properties configProperties;

    /**
     * reads values from config property file
     *
     * @throws Exception throws, if fails
     */
    public static void loadConfigProperties() throws Exception {
        Properties properties = new Properties();
        InputStream inputStream = null;
        try {
            File file = new File((new File(".")).getCanonicalPath()
                    + File.separator + "resources" + File.separator
                    + "config.properties");
            if (file.exists()) {
                inputStream = new FileInputStream(file);
            } else {
                String msg = "File does not exist : " + "config.properties";
                System.out.println(msg);
            }
        } catch (FileNotFoundException e) {
            String msg = "File can not be found : " + "config.properties";
            System.out.println(msg);
            throw new Exception(msg, e);
        } catch (IOException e) {
            String msg = "Can not create the canonical file path for given file : "
                    + "config.properties";
            System.out.println(msg);
            throw new Exception(msg, e);
        }

        try {
            if (inputStream != null) {
                properties.load(inputStream);
            }
        } catch (IOException e) {
            String msg = "Error loading properties from config.properties file";
            System.out.println(msg);
            throw new Exception(msg, e);
        } finally {
            try {
                if (inputStream != null) {
                    inputStream.close();
                }
            } catch (IOException ignored) {
                System.out.println("Error while closing input stream");
            }
        }
        configProperties = properties;
    }
}

Related

  1. loadBuildProperties(File projectRootDir)
  2. loadCfg(String url)
  3. loadConfig(File settingsFile)
  4. loadConfigByPath(String path)
  5. loadConfigProperties()
  6. loadConfiguration()
  7. loadConfiguration(String file)
  8. loadConfiguration(String filePath)
  9. loadCredentials(File credentials, String credentialsAppend)