logo
Java Properties configuration file
Reading the file
Writing in the file
And for XML what happens?

Java Properties configuration file

We will see how to create and use a simple setup file for a Java software. The configuration file will contain keys and associated values. It will be possible to put comment lines beginning these with '#'. It should be noted that "Properties extends Hashtable", in fact it work similarly. With the addition of the load(InputStream is) and store(InputStream is) methods. And it is even possible to do XML for the unconditional ones. With the methods and loadToXML StoreToXML.

Reading the file

Here is an example configuration file:
#this is the property config file
stuff="i am the stuff"
Now we will try to get the value associated with the words "stuff" and "thing". On failure, return a default value.
Here is a small example:
public static void main(String[] args) {
 String configPath="config";
 Properties properties=new Properties();
 try {
 FileInputStream in =new FileInputStream(configPath);
 properties.load(in);
 in.close();
 } catch (IOException e) {
 System.out.println("Unable to load config file.");
 }
 
 //let's do the magic
 System.out.println(properties.getProperty("stuff", "defaultStuff"));
 System.out.println(properties.getProperty("thing", "defaultThing"));
}
You simply open and load the file, and you have a hash table :).

Writing in the file

It is possible to save the configuration. To do this, add this code to the end of the "main" that uses the method "store" to save our config.
properties.setProperty("newStuff", "i am the new stuff :)");
 FileOutputStream out;
 try {
 out = new FileOutputStream(configPath);
 properties.store(out, "---config---");
 out.close();
 } catch (IOException e) {
 System.err.println("Unable to write config file.");
 }

And for XML what happens?

Almost the same, replace "store" by "storeToXML". Execute, look at your config file... now XML :D. Replace "load" with "LoadFromXML" to fix it.
public static void main(String[] args) {
 String configPath="config";
 Properties properties=new Properties();
 //reading stuff
 try {
 FileInputStream in =new FileInputStream(configPath);
 properties.loadFromXML(in);
 in.close();
 } catch (IOException e) {
 System.err.println("Unable to load config file.");
 }
 
 //let's do the magic
 System.out.println(properties.getProperty("stuff", "defaultStuff"));
 System.out.println(properties.getProperty("thing", "defaultThing"));
 
 //reading stuff
 properties.setProperty("newStuff", "i am the new stuff :)");
 FileOutputStream out;
 try {
 out = new FileOutputStream(configPath);
 properties.storeToXML(out, "---config---");
 out.close();
 } catch (IOException e) {
 System.err.println("Unable to write config file.");
 }
}
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">
<properties>
<comment>---config---</comment>
<entry key="stuff">That is cool stuff</entry>
<entry key="newStuff">i am the new stuff :)</entry>
</properties>
Be The First To Comment.

Add a comment:





You can use these HTML tags: <br><a><i><b>