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

» How to Change File Association in Windows

This guide is valid for Windows XP, Windows Vista, and Windows 7.  Windows holds a default program that should be used to open each type of file on your computer.  Many times, installing a new program (such as iTunes)  that handles certain file types will automatically change what file types it will open by default and often this is configurable after installation as well.  However, you can manually change the program Windows uses with the following steps.Click on Start...

» Customizing File Association in Emacs (based on file extension)

Occassionally it can be useful to have emacs automatically use a particular syntax highlighter based on file extension.  Typically this is useful when you want to use non-stnadard file extensions in your own environment.  You can get it to work by adding the following to your .emacs file (typically located in your home directory):;; Use perl syntax highlighter for .foo files.(setq auto-mode-alist (cons '(".*\.foo" . cperl-mode) auto-mode-alist))The above code would use the cperl...

» How to Send Mail in PHP

The BasicsPHP provides a handy function for mailing from a server.  On a basic level, all you need to do is call the function mail with three parameters, which has the following syntax.bool (string $to, string $subject, string $message [, string $headers][, string $additonal_params]]) These parameters are pretty straight forward.  Below is an example of how to send a very simple email message with no headers.1234$to = "example@wisc.edu";$subject = "This is an example email.";$message...

Comments  

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