In Java, property files has very important role to implement flexible applications that can be configured at run time without rebuilding the application. Today, we will see how to manage property files to read key value pairs or to write key values pairs and also to walk through all properties even when you do not know the name of the keys.

In our example, we have a property file named as “example.properties” in which we will first put some properties and then read all the properties from file again. After that we will print all read key value in console.

Here is the code:

 Java |  copy code |? 
01
02
package com.raistudies;
03
 
04
import java.io.FileInputStream;
05
import java.io.FileNotFoundException;
06
import java.io.FileOutputStream;
07
import java.io.IOException;
08
import java.util.Iterator;
09
import java.util.Properties;
10
 
11
public class PropertyFileManager {
12
 private FileOutputStream outStream;
13
 private FileInputStream inputStream;
14
 private Properties properties = new Properties();
15
 
16
 public void saveProperties(String file){
17
 try {
18
 outStream = new FileOutputStream(file);
19
 properties.store(outStream, "Saving properties to file");
20
 
21
 } catch (FileNotFoundException e) {
22
 e.printStackTrace();
23
 } catch (IOException e) {
24
 e.printStackTrace();
25
 }
26
 }
27
 
28
 public void readProperties(String file){
29
 try {
30
 inputStream = new FileInputStream(file);
31
 properties.load(inputStream);
32
 } catch (FileNotFoundException e) {
33
 e.printStackTrace();
34
 } catch (IOException e) {
35
 e.printStackTrace();
36
 }
37
 }
38
 
39
 public static void main(String[] args) {
40
 String file = "example.properties";
41
 String key;
42
 PropertyFileManager manager = new PropertyFileManager();
43
 
44
 // Writing to property file
45
 manager.properties.setProperty("Name", "Rahul Mondal");
46
 manager.properties.setProperty("City", "Jaipur");
47
 manager.properties.setProperty("Company", "HCL");
48
 manager.saveProperties(file);
49
 
50
 //Getting properties from property file
51
 manager.properties = new Properties();
52
 manager.readProperties(file);
53
 
54
 //Printing all key value pairs
55
 Iterator iterator = manager.properties.keySet().iterator();
56
 while(iterator.hasNext()){
57
 key = (String)iterator.next();
58
 System.out.println("Key : " + key + " ; Value :" +manager.properties.getProperty(key));
59
 }
60
 
61
 }
62
 
63
}
64
 
65

As you can see, the class PropertyFileManager contains two methods, one for saving properties to the file and other for reading properties from property file.

Saving the properties to Property file

   To save property:

  1. First of all we have added the properties to be saved to the instance of Properties class using the method setProperty().
  2. Then, by calling the method store(), we save the properties to file. We have to pass instance of FileOutputStream of the file to the store() method.

Reading the properties from Property File

   To read properties:

  1. To read the properties from file, first create an instance of FileInputStream of the file from which you want to read the properties.
  2. Then class the method load() of the class Properties by passing the instance of FileInputStream to it. All properties stored in file will be loaded in to the instance of Properties class

Output of the above program:

Key : Name ; Value :Rahul Mondal
Key : City ; Value :Jaipur
Key : Company ; Value :HCL