Properties File Java Example
In this example, we shall show you how to use property files in java. Java property files are an easy and effective way
to organize key value pairs in plain text files. Property files support two formats
- Text file format – In this format, different key, value pairs are separated by newline character
- XML format – This is standard XML format.
Java gives support to read and write property files via java.util.Properties
class. This class extends
java.util.HashTable
class and therefore supports key value pairs inherently.
Example in this post is created using Eclipse Luna and Java7.
1. Properties constructors
Properties class supports 2 constructors
Properties()
Default constructor. This is used to create an empty properties object. This properties object will not contain any
properties on time of its creation
Properties(Properties defaults)
This is used to create Properties object with a default set of properties.
2. Properties common methods
Following are some useful methods from properties class
String getProperty(String key)
String getProperty(String key,String default)
void load(InputStream reader)
void loadFromXML(InputStream reader)
2.1 Reading properties
Reads property with a given key, returns value of the property. This method returns null if property is not
found.
Reads property with a given key, returns value of the property. This method returns default parameter passed
in if property is not found.
Loads properties from a java.io.InputStream instance.
Loads XML document and parse it to populate properties.
Object setProperty(String key, String value)
void store(OutputStream out,String comments)
void storeToXML(OutputStream os,String comment)
2.2 Writing properties
Sets property as key, value pair in Properties object. This method returns previously set value of the property
if any
Stores the current property object into out OutputStream. Comments will be added into property file.
Stores properties in XML document using passed in OutputStream. Comment will be added as a comment in the
document.
set stringPropertyNames()
2.3 Iterating over properties
Returns a java.util.Set of Strings, These can be iterated over using java standard for each loop.
3. Example of using Properties in java
PropertiesExample.java
package com.javacodegeeks; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.util.Properties; public class PropertiesExample { - public static void main(String[] s) { //////saving properties into example.properties file///////// try (OutputStream out = new FileOutputStream("example.properties")) { Properties properties = new Properties(); properties.setProperty("name", "javaCodeGeeks"); properties.setProperty("article", "JavaProperties"); properties.setProperty("version", "1.0"); properties.setProperty("ide", "eclipse"); properties.store(out, "This is a sample for java properties"); } catch (IOException e) { e.printStackTrace(); } /////////////////////////////////////////////////////////////// ////////////Reading properties//////////////////////////////// try (InputStream in = new FileInputStream("example.properties")) { Properties prop = new Properties(); prop.load(in); System.out.println("####Properties.getProperty usage####"); System.out.println(prop.getProperty("name")); System.out.println(); System.out.println("####Properties.stringPropertyNames usage####"); for (String property : prop.stringPropertyNames()) { String value = prop.getProperty(property); System.out.println(property + "=" + value); } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } System.out.println(); //////////////////////////////////////////////////// /////////writing and reading fromxml//////////////// try (OutputStream out = new FileOutputStream("example.xml")) { Properties properties = new Properties(); properties.setProperty("name", "javaCodeGeeks"); properties.setProperty("article", "JavaProperties"); properties.setProperty("version", "1.0"); properties.setProperty("ide", "eclipse"); properties.storeToXML(out, "This is how we can have properties as xml"); } catch (IOException e) { e.printStackTrace(); } /////////////////////////////////////////////////////// ///////////Reading properties from xml///////////////// try (InputStream in = new FileInputStream("example.xml")) { Properties prop = new Properties(); prop.loadFromXML(in); System.out.println("####Properties.load from xml usage####"); for (String property : prop.stringPropertyNames()) { String value = prop.getProperty(property); System.out.println(property + "=" + value); } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } System.out.println(); /////////////////////////////////////////////////////// } }
4. Output
####Properties.getProperty usage#### javaCodeGeeks ####Properties.stringPropertyNames usage#### version=1.0 name=javaCodeGeeks ide=eclipse article=JavaProperties ####Properties.load from xml usage#### version=1.0 name=javaCodeGeeks ide=eclipse article=JavaProperties
5. Download source code
This was an example of java.util.properties
.
You can download the full source code of this example here : JavaPropertiesExample.zip