Jan
02
|
The Properties class has a store() method for saving a set of properties to a stream. There are lots of examples on the web that show how to use store() but unfortunately the majority of them fail to close the stream which is a bit misleading for new developers. Closing the stream is the responsibility of the caller (as mentioned in the javadoc), and failure to do it can result in the properties not being written to their ultimate destination.
Here’s an example showing how to write the properties to a file (including closing the stream).
Properties prop = new Properties(); OutputStream out = new FileOutputStream("my.properties"); try { prop.setProperty("abc", "123"); prop.store(out, null); } catch (IOException ex) { ex.printStackTrace(); } finally { out.close(); }
Array ( ) One Response to “How to store values in a properties file”
Leave a Reply
You must be logged in to post a comment.
February 3rd, 2011 at 2:08 am
Thank you very much for this advice! It’s a “small” thing but very important! :)))))