This short article shows you how to read and write value to properties file in Java.
1. Import java.util.Properties
import java.util.Properties;
2. The code snippet to write to properties file as below.
try (OutputStream out = new FileOutputStream(“example.properties”)) {
Properties properties = new Properties();
properties.setProperty(“name”, “mingman”);
3. The snippet to read to properties file as below.
try (InputStream in = new FileInputStream(“example.properties”)) {
Properties prop = new Properties();
prop.load(in);
System.out.println(“####Properties.getProperty usage####”);
System.out.println(prop.getProperty(“name”));
System.out.println();
System.out.println(“####Properties.stringPropertyNames usage####”);
for (String property : prop.stringPropertyNames()) {
String value = prop.getProperty(property);
System.out.println(property + “=” + value);
}
This code is extracted from http://examples.javacodegeeks.com/core-java/util/properties/properties-file-java-example/.
You can also download a single solution from http://skydrive.live.com. The sample file name is JavaPropertiesExample.zip. My MSN ID is chanmmn@hotmail.com.