Header javaperspective.com
JavaPerspective.com  >   Advanced Tutorials  >   4. Miscellaneous Java features  >   4.6. Properties files

4.6. Properties files
Last updated: 1 February 2013.

This tutorial discusses properties files in Java.

A properties file is a file containing an application's configuration information in the form of key/value pairs. For example, an application that uses a database can retrieve the database connection URL, username and password from a properties file at startup. The class Properties provides methods to load a properties file, retrieve its values, update them and also write the properties list (the key/value pairs) to a file.


4.6.1. How to create a properties file from Java code

To create a properties file, just instantiate a Properties object, populate it with key/value pairs (method setProperty) and call one of the methods store or storeToXML. As an example, the following sample shows how to set up a properties file containing database connection information:

FileOutputStream out = null;

try {
   
// Instantiate a Properties object
   
Properties properties = new Properties();

   
// Populate it with key/value pairs
   
properties.setProperty("url", "jdbc:oracle:thin:@localhost:1521:orcl");
    properties.setProperty
("userName", "javaholic");
    properties.setProperty
("userPassword", "javaholic");

   
// Create the output stream
   
out = new FileOutputStream("properties.xml");

   
// Store the properties to the output stream
   
properties.storeToXML(out, "Database connection information");
}
catch (IOException e) {
   
e.printStackTrace();
}
finally{
   
try {
         
// Close the output stream
         
if(out != null)
               
out.close();
   
}
   
catch (IOException e) {
         
e.printStackTrace();
   
}
}

Here is the generated XML file:

   <?xml version="1.0" encoding="UTF-8" standalone="no"?>
   <!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">
   <properties>
   <comment>Database connection information</comment>
   <entry key="url">jdbc:oracle:thin:@localhost:1521:orcl</entry>
   <entry key="userPassword">javaholic</entry>
   <entry key="userName">javaholic</entry>
   </properties>


4.6.2. How to load a properties file

The key/value pairs contained in the file shown above can be loaded into a Properties object by calling the method loadFromXML as shown in the next sample:

FileInputStream in = null;

try{
   
Properties properties = new Properties();

    in =
new FileInputStream("properties.xml");

    properties.loadFromXML
(in);

    String url = properties.getProperty
("url");
    String userName = properties.getProperty
("userName");
    String userPassword = properties.getProperty
("userPassword");

   
// Establish a database connection with url, userName and userPassword
    // ...
}
catch (IOException e) {
   
e.printStackTrace();
}

To load the properties, I have used the method loadFromXML because the properties file was saved with the method storeToXML. If the file had been saved with the method store, I would have had to use the method load instead. You might have noticed that I have not closed the FileInputStream object. In fact, it is closed after loadFromXML returns.

As you can see, properties files are very easy to manipulate in Java. Generally, applications store their configuration information in one or more properties files. That way, changes in the application's configuration do not affect the code, which is highly recommended.


You are here :  JavaPerspective.com  >   Advanced Tutorials  >   4. Miscellaneous Java features  >   4.6. Properties files
Next tutorial :  JavaPerspective.com  >   Advanced Tutorials  >   5. Best practice tips

Copyright © 2013 - 2016. JavaPerspective.com. All rights reserved.  ( Terms | Contact | About ) 
Java is a trademark of Oracle Corporation
Image 1 Image 2 Image 3 Image 4 Image 5 Image 6 Image 7