How to write properties file in java

In this example we will discuss how to write property file in java. In this example We have write data to properties file. The properties file has fundamentally used to store the data configuration data or settings. We have store data key value pair. Java has provide java.util.Properties class and Properties class to store data into .properties file.

How to write properties file in java

In this example we will discuss how to write property file in java. In this example We have write data to properties file. The properties file has fundamentally used to store the data configuration data or settings. We have store data key value pair. Java has provide java.util.Properties class and Properties class to store data into .properties file.

Write properties file in java

Write properties file in java

In this example we will discuss how to write property file in java. In this example We have write data to properties file. The properties file has fundamentally used to store the data configuration data or settings. We have store data key value pair. Java has provide java.util.Properties class and Properties class to store data into .properties file.

Program Description:-

In this example we have created Properties class and we have put three key/value pairs and every key/value pairs are both String values. We have used setProperties() method.We have store values setProperty(String key, String value) method Calls the Hashtable method put And we have use FileOutputStream method. This method we can used to be write or store the values in properties file. The OutputStream we used when load() the method in a program.

Example:-

import java.io.*;
import java.util.Properties;
public class WriteProperties {
        public static void main(String[] args) {
        	Properties file = new Properties();    
        	try {
                        
                        File file2 = new File("c://write.properties");
                        FileOutputStream fos = new FileOutputStream(file2);
                        file.setProperty("database", "localhost");
                        file.setProperty("userName", "Naulej");
                        file.setProperty("Password", "naulej");
                        file.store(fos, "");
                        fos.close();
                } catch (FileNotFoundException e){
                        e.printStackTrace();
                } catch (IOException e) {
                        e.printStackTrace();
                }
        }
}

How to Run on command prompt

Output Display setProperties values

Download Source Code