Java Properties Load loadProperties()

Here you can find the source of loadProperties()

Description

Attempt to open the default properties file and load its contents.

License

Open Source License

Return

true if properties file is opened and loaded successfully, false otherwise

Declaration

public static boolean loadProperties() 

Method Source Code


//package com.java2s;

import java.io.FileInputStream;

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

public class Main {
    /** properties read from properties file */
    private static Properties props = null;

    /**/*from  w ww.j  av  a 2s  .  c  om*/
     * Attempt to open the default properties file and load its contents.
     * @return true if properties file is opened and loaded successfully, false otherwise
     */
    public static boolean loadProperties() {
        return loadProperties(null, null);
    }

    /**
     * Attempt to open a properties file and load its contents.
     * @param strPropertiesFilePath path to properties file, default = "{user.home}/Application Data/UMassLowellCS"
     * @param strPropertiesFileName name of properties file, default = "CompositionProject.properties" ;
     * @return true if properties file is opened and loaded successfully, false otherwise
     */
    public static boolean loadProperties(String strPropertiesFilePath, String strPropertiesFileName) {

        // String to pass to the FileInputStream constructor
        String strPropertiesFile = constructPropertiesFileString(strPropertiesFilePath, strPropertiesFileName);

        // create Properties object if it does not exist
        if (props == null) {
            props = new Properties();
        }

        try {
            // load properties from file
            FileInputStream in = new FileInputStream(strPropertiesFile);
            props.load(in);
            in.close();
            return true;
        } catch (IOException ioe) {
            // System.out.println(ioe.getMessage());
            return false;
        }
    }

    /**
     * Construct the full file reference for the properties file to be loaded or saved.
     * @param strPropertiesFilePath path to properties file, default = "{user.home}/Application Data/UMassLowellCS"
     * @param strPropertiesFileName name of properties file, default = "CompositionProject.properties" ;
     * @return String to pass to the FileInputStream or FileOutputStream constructor
     */
    static String constructPropertiesFileString(String strPropertiesFilePath, String strPropertiesFileName) {

        // if no path is defined for the properties file, set it to the
        //    UMassLowellCS directory
        if (strPropertiesFilePath == null) {
            strPropertiesFilePath = System.getProperty("user.home") + System.getProperty("file.separator")
                    + "Application Data" + System.getProperty("file.separator") + "UMassLowellCS";
        }

        // if no name is defined for the properties file, set it to a generic name
        // if a file name is defined but it does not have an extension of .properties,
        //    append that extension
        if (strPropertiesFileName == null) {
            strPropertiesFileName = "CompositionProject.properties";
        } else if (!strPropertiesFileName.toLowerCase().endsWith(".properties")) {
            strPropertiesFileName += ".properties";
        }

        // return the full constructed String to pass to the FileInputStream or
        //    FileOutputStream constructor
        return strPropertiesFilePath + System.getProperty("file.separator") + strPropertiesFileName;
    }
}

Related

  1. loadProperties()
  2. loadProperties()
  3. loadProperties()
  4. loadProperties()
  5. loadProperties()
  6. loadProperties()
  7. loadProperties()