How to use java Properties API -- read and write
===========================Source Code============================================
package suncertify.utility;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.Properties;
import java.util.Set;
import java.util.logging.LogManager;
import suncertify.Constraints;
import suncertify.db.SystemException;
public class ConfiguraionUtil {
public static final Properties readProperties()
throws SystemException {
Properties properties = new Properties();
try {
properties.load(new FileInputStream(
new File(Constraints.FILE_PROPERTIES)));
} catch (FileNotFoundException e) {
throw new SystemException(
"Can not find the suncertify.properties file");
} catch (IOException e) {
throw new SystemException("I/O error occurs when reading the file");
}
return properties;
}
public static final void writeProperties(final Hashtable table)
throws SystemException {
Properties properties = new Properties();
FileOutputStream out = null;
try {
Set keys = table.keySet();
Iterator iter = keys.iterator();
String content = "";
while (iter.hasNext()) {
String key = (String) iter.next();
Object value = table.get(key);
content += key + "=" + value + "/n";
}
out = new FileOutputStream(new File(Constraints.FILE_PROPERTIES));
out.write(content.getBytes());
properties.store(out, null);
} catch (FileNotFoundException e) {
throw new SystemException(
"Can not find the suncertify.properties file");
} catch (IOException e) {
throw new SystemException("I/O error occurs when reading the file");
} finally {
try {
out.close();
} catch(IOException e) {
out = null;
}
}
}
}
Reference:
http://www.dimi.uniud.it/labs/documentazione/java1.0-tutorial/java/cmdLineArgs/properties.html