Java Properties Save saveProperty(String key, String value)

Here you can find the source of saveProperty(String key, String value)

Description

save Property

License

Apache License

Declaration

public static void saveProperty(String key, String value) 

Method Source Code


//package com.java2s;
//License from project: Apache License 

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Properties;

public class Main {
    public static void saveProperty(String key, String value) {
        Properties prop = getProperties();
        OutputStream output = null;

        try {/* w  w  w  . j ava  2  s  . c o  m*/
            output = new FileOutputStream("config.properties");
            // set the properties value
            prop.setProperty(key, value);
            // save properties to project root folder
            prop.store(output, null);
        } catch (IOException io) {
            io.printStackTrace();
        } finally {
            if (output != null) {
                try {
                    output.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

        }
    }

    private static Properties getProperties() {
        Properties prop = new Properties();
        InputStream input = null;

        if (!new File("config.properties").exists())
            return prop;

        try {
            input = new FileInputStream("config.properties");
            // load a properties file
            prop.load(input);
            // get the property value and print it out
            return prop;

        } catch (IOException ex) {
            ex.printStackTrace();
        } finally {
            if (input != null) {
                try {
                    input.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return prop;
    }
}

Related

  1. saveProperties(String propName, Properties props)
  2. savePropertiesToEncodedString(Properties props, String comment)
  3. savePropertiesToString(Properties props, String comment)
  4. saveProperty(String key, String value)
  5. saveProperty(String key, String value)
  6. saveProps(Properties p, String fname, String comment)
  7. saveProps(String path, Properties props)
  8. saveSorted(Properties props, File file)
  9. saveSysDirProperties(Properties sysProps, String classpathDirectory)