PropTest.java
import java.io.*;
import java.util.Properties;
/**
Demonstration of Properties API.
*/
class PropTest
{
public static void main(String argv[])
{
try {
// Create some default properties.
Properties defaults = new Properties();
defaults.put("Bashful","dwarf 1");
defaults.put("Doc","dwarf 2");
defaults.put("Dopey","dwarf 3");
defaults.put("Grumpy","dwarf 4");
defaults.put("Happy","dwarf 5");
defaults.put("Sleepy","dwarf 6");
defaults.put("Sneezy","dwarf 7");
// Create a new Properties set based on the default set.
Properties settings = new Properties(defaults);
settings.put("font","Courier");
settings.put("size","10");
settings.put("message","Hello, handsome!");
// Fetch using the default.
System.out.println("Sneezy is " +
settings.getProperty("Sneezy","<nothing>"));
System.out.println("Font is " +
settings.getProperty("font","12"));
// Write the Properties object to a file.
// (Note that the file will *not* contain the defaults!)
FileOutputStream out =
new FileOutputStream("dummy.prop");
settings.store(out,"Heading for property file");
// Create another set...
Properties other = new Properties();
// ...and fill it from the file.
FileInputStream in =
new FileInputStream("dummy.prop");
other.load(in);
// Change the second set...
other.put("font","Zapf Dingbats");
other.put("size","72");
// ...and write it to a second file.
FileOutputStream out2 =
new FileOutputStream("dummy2.prop");
other.store(out2,"YAH (Yet Another Heading)");
}
catch (Exception e) {
System.out.println("Exception: " + e);
}
}
}