Save user settings in Java application

Class:

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;
 
 
public class configManager {
    private Properties _properties;
    private final String _filename = "my.properties";
 
    public configManager(){
        this._properties = new Properties();
        File file=new File(this._filename);
 
        if (!file.exists()) {
            try {
                file.createNewFile();
            } catch (IOException ex) { System.err.println(ex.toString()); }
        }
 
        try {
            this._properties.load(new FileInputStream(this._filename));
        } catch (IOException e) { System.err.println(e.toString()); }
    }
 
    /**
     * read property
     * @param key
     * @return String
     */
    public String read(String key){
        return this._properties.getProperty(key);
    }
 
    /**
     * write property and save
     * @param key
     * @param value 
     */
    public void write(String key, String value){
        this._properties.setProperty(key, value);
        try {
            this._properties.store(new FileOutputStream(this._filename), null);
        } catch (IOException e) { System.err.println(e.toString()); }
    }
}

Usage:

configManager configM = new configManager();
String path = configM.read("mypath");
path = newpath;
configM.write("mypath", path);
%d bloggers like this: