Java Properties Save saveProperties()

Here you can find the source of saveProperties()

Description

Attempt to save the properties file.

License

Open Source License

Declaration

public static void saveProperties() 

Method Source Code


//package com.java2s;
import java.io.File;

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

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

    /**/*from   ww w  .  j  a  v a  2 s  .  com*/
     * Attempt to save the properties file.
     */
    public static void saveProperties() {
        saveProperties(null, null, null);
    }

    /**
     * Attempt to save the properties file.
     * @param strPropertiesFilePath path to properties file, default = "user.home/Application Data/UMassLowellCS"
     * @param strPropertiesFileName name of properties file, default = "GUIProgramming.properties" ;
     * @param strPropertiesTitle title to write into properties file, default = "GUI Programming"
     */
    public static void saveProperties(String strPropertiesFilePath, String strPropertiesFileName,
            String strPropertiesTitle) {

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

        // set default properties title
        if (strPropertiesTitle == null) {
            strPropertiesTitle = "GUI Programming";
        }

        // isolate the properties file path
        if (strPropertiesFilePath == null) {
            strPropertiesFilePath = strPropertiesFile.substring(0,
                    strPropertiesFile.lastIndexOf(System.getProperty("file.separator")));
        }

        // if the properties file path does not exist, create it
        File filePropertiesFilePath = new File(strPropertiesFilePath);
        if (!filePropertiesFilePath.exists()) {
            filePropertiesFilePath.mkdirs();
        }

        // save properties file
        // reference: Deitel & Deitel, Java How To Program, 6th ed., p. 948
        try {
            File file = new File(strPropertiesFile);
            FileOutputStream out = new FileOutputStream(file);
            props.store(out, strPropertiesTitle);
            out.close();
        } catch (FileNotFoundException fnfe) {
            System.err.println(fnfe);
        } catch (IOException ioe) {
            System.err.println(ioe);
        }
    }

    /**
     * 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. saveMailAtt(String host, String userName, String password, String from, String directory)
  2. saveOccurrencesAsText(String fileName, TreeMap distribution, int frequency, char[] ignore)
  3. saveOutputFile(String prefix, String suffix, String data)
  4. saveParamsToFile(String fileName, String[] params)
  5. saveParamToFile(String fileName, String param, String value)
  6. saveProperties(final IResource modelResource, final Properties props)
  7. saveProperties(Map map, File file, String encoding)
  8. saveProperties(Map map, Writer tmpWriter)
  9. saveProperties(Properties p, String file)