Java Properties Save saveProperty(String key, String value)

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

Description

save Property

License

Open Source License

Declaration

private static void saveProperty(String key, String value) 

Method Source Code


//package com.java2s;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
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 {
    private static void saveProperty(String key, String value) {
        Properties properties = loadUserSettings();
        properties.setProperty(key, value);
        saveUserSettings(properties);//from   w w w  . ja  v  a  2  s . c  o  m
    }

    private static Properties loadUserSettings() {
        String filename = System.getProperty("user.home") + File.separator + ".RacePoint.xml";
        Properties result = new Properties();
        InputStream in = null;
        try {
            in = new FileInputStream(filename);
            BufferedInputStream inBuf = new BufferedInputStream(in);
            result.loadFromXML(inBuf);
            inBuf.close();
        } catch (IOException e) {
            result = new Properties();
        } finally {
            if (in != null) {
                try {
                    in.close();
                } catch (IOException e) {
                }
            }
        }
        return result;
    }

    private static void saveUserSettings(Properties properties) {
        String filename = System.getProperty("user.home") + File.separator + ".RacePoint.xml";
        OutputStream out = null;
        try {
            out = new FileOutputStream(filename);
            BufferedOutputStream outBuf = new BufferedOutputStream(out);
            properties.storeToXML(outBuf, "User settings for RacePoint");
            outBuf.close();
        } catch (IOException e) {
        } finally {
            if (out != null) {
                try {
                    out.close();
                } catch (IOException e) {
                }
            }
        }
    }
}

Related

  1. saveProperties(String fileName, Properties properties)
  2. saveProperties(String fileName, Properties properties, String title)
  3. saveProperties(String propName, Properties props)
  4. savePropertiesToEncodedString(Properties props, String comment)
  5. savePropertiesToString(Properties props, String comment)
  6. saveProperty(String key, String value)
  7. saveProperty(String key, String value)
  8. saveProps(Properties p, String fname, String comment)
  9. saveProps(String path, Properties props)