Java: how to read and write a properties file 

Joined:
04/09/2007
Posts:
565

January 29, 2010 23:40:11
Load from specified PATH
// import java.util.Properties
Properties p = new Properties();

in = new FileInputStream("prog.properties"); // relative path
// or absolute path
// in = new FileInputStream("C:/tmp/prog.properties");
try {
    p.load(in);
}
finally {
    in.close();
}


Load from CLASSPATH
Properties p = new Properties();

// Load from classpath
// This method delegates to this object's class loader. If this 
// object was loaded by the bootstrap class loader, the method 
// delegates to ClassLoader.getSystemResourceAsStream
in = this.getClass().getResourceAsStream("prog.properties");
try {
    p.load(in);
}
finally {
    in.close();
}


Write Properties object to a file
// Write properties file.
out = new FileOutputStream("prog.properties");
try {
    p.store(out, "add a comment");
} 
finally {
    out.close();
}
[ Comment  | Tags ]
 
Easy email testing with http://www.ximailstop.com