|
How to read and write a Properties file |
|
|
This Java tip illustrates a method of reading and writing a Properties file.
Developer may read by using input stream and write by using output stream.
Here is an example of the contents of a properties file:
# a comment
! a comment
a = a string
b = a string with escape sequences \t \n \r \\ \" \' \ (space) \u0123
c = a string with a continuation line \
continuation line
d.e.f = another string
// Read properties file.
Properties prop = new Properties();
try {
prop.load(new FileInputStream("filename.properties"));
} catch (IOException e) {
}
// Write properties file.
try {
prop.store(new FileOutputStream("filename.properties"), null);
} catch (IOException e) {
}
|
Related Tips
|