Java - Reading and writing a properties file

(0 votes, average 0 out of 5)

A properties file in Java can be very useful when you start to notice that there are many locations in your code where you are either hard coding values, many constants, or using enums. Instead of including these values directly in your code, you can refactor them out to a properties file to separate responsibilities. Instead of potentially specifying values throughout many classes or programs, all values can be condensed into one or more properties files as need.

An example of a properties file is as follows:

 

# 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

 

 

A properties file can be read or conversely written as follows:

 

// Read properties file.
Properties properties = new Properties();
try {
    properties.load(new FileInputStream("filename.properties"));
} catch (IOException e) {
}
 
// Write properties file.
try {
    properties.store(new FileOutputStream("filename.properties"), null);
} catch (IOException e) {
}

 

 

Setting or retrieving values from an existing properties file can be performed as follows:

 

String string = properties.getProperty("a.b");
properties.setProperty("a.b", "new value");

 

That's it! Properties files are easy to create and work with and can greatly aid in organizing and cleaning up your code.
Partner Links:
Last Updated on Monday, 12 July 2010 07:56  
Related Articles

» Search and Replace in Emacs

Searching and replacing in emacs is pretty striaghtforward.  The simplest and easiest way to do a query search and replace is to press M-% (typically this is Alt+Shift+5).  You will be prompted to first enter the search string (the string to replace), and then the replacement string.  Emacs will then begin an iterative search; for each selection, press one of the following:y to accept the replace and continuen to deny the replace and continue!  to automatically replace all...

» Mount a WebDAV filesystem (like box.net) on Linux using davfs2

You can easily mount a WebDAV filesystem on Linux so that you can access the files just like they were on your local computer. One way to do is to install the davfs2 package (this is the Ubuntu/Debian package name, though it should be similar for other distros). Once you have installed it, add an entry to /etc/davfs2/secrets with your login credentials for the WebDAV account. For box.net, the command would look like this:sudo echo “https://www.box.net/dav username password” >>...

» Oracle 9.2.0 Error: System.Data.OracleClient requires Oracle client software version 8.1.7

ProblemWhen using a version of Oracle 9.2.0 in ASP.NET, attempting to access a site which uses System.Data.OracleClient may give an error with the following exception:  System.Data.OracleClient requires Oracle client software version 8.1.7 or greater This is a common error when using ASP.NET with Oracle -- it is in fact a bug in the default Oracle 9.2.0 installation permissions.  To fix the error, follow the following solution steps (taken from cited source): Log on to...

Comments  

 
-1 #1 Patrick DeLine 2010-08-26 11:31
Good article, Jason - this should be essential reading for every new computer science student...