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

Return

return properties

Declaration

public static Properties loadConfigProperties() 

Method Source Code


//package com.java2s;
/*//from  w  w w. ja v  a 2s  .c  o m
*  Copyright (c) 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 {
    /**
     * reads values from config property file
     * @return return properties
     */
    public static Properties loadConfigProperties() {

        Properties properties = new Properties();
        InputStream inputStream = null;
        try {
            File file = new File((new File(".")).getCanonicalPath() + File.separator + "src" + File.separator
                    + "main" + File.separator + "resources" + File.separator + "config.properties");
            if (file.exists()) {
                inputStream = new FileInputStream(file);
            } else {
                System.err.println("File does not exist : " + "config.properties");
            }
        } catch (FileNotFoundException e) {
            System.err.println("File can not be found : " + "config.properties");
            e.printStackTrace();
        } catch (IOException e) {
            System.err.println("Can not create the canonical file path for given file : " + "config.properties");
            e.printStackTrace();
        }

        try {
            if (inputStream != null) {
                properties.load(inputStream);
            }
        } catch (IOException e) {
            System.err.println("Error loading properties from config.properties file");
            e.printStackTrace();
        } finally {
            try {
                if (inputStream != null) {
                    inputStream.close();
                }
            } catch (IOException ignored) {
                System.err.println("Error while closing input stream");
            }
        }

        if (properties.isEmpty()) {
            System.out.println("No configurations are loaded.  Using default configurations");
        }
        return properties;
    }
}

Related

  1. loadApiProperties(InputStream inputStream)
  2. loadBuildProperties(File projectRootDir)
  3. loadCfg(String url)
  4. loadConfig(File settingsFile)
  5. loadConfigByPath(String path)
  6. loadConfigProperties()
  7. loadConfiguration()
  8. loadConfiguration(String file)
  9. loadConfiguration(String filePath)