Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Home Java Example Java Core How to write properties file in java

Related Tutorials


 
 

Share on Google+Share on Google+

How to write properties file in java

Advertisement
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

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

Advertisement

If you enjoyed this post then why not add us on Google+? Add us to your Circles



Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: July 26, 2013

Related Tutorials

Discuss: Write properties file in java  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:0
DMCA.com