Create Property File in Java
This example program will teach you to learn, How to create property file in java. The example program were attached in the post.
Project Structure
Example Program (CreatePropertyFile.java)
package com.dineshkrish.file; import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.util.Properties; /** * * @author Dinesh Krishnan * */ public class CreatePropertyFile { public static void main(String[] args) { // defining the properties object Properties properties = new Properties(); // setting the properties properties.setProperty("first.name", "Dinesh"); properties.setProperty("second.name", "Krishnan"); properties.setProperty("full.name", "Dinesh Krishnan"); properties.setProperty("country", "India"); // defining the file object File file = new File("myproperty.properties"); try { // defining the FileOutputStream object FileOutputStream outputStream = new FileOutputStream(file); // storing the properties as file with comments properties.store(outputStream, "This property file contains Dinesh Krishnan information"); System.out.println("Property file has been created successfully..."); } catch (FileNotFoundException e) { System.out.println(e.getMessage()); e.printStackTrace(); } catch (IOException e) { System.out.println(e.getMessage()); e.printStackTrace(); } } }
Output
Once you compile and run the above example program, After the execution of the program it will produce the property file as a output with the properties that are mentioned. The sample output you can see below.
#This property file contains Dinesh Krishnan information
#Fri Jan 06 16:36:06 IST 2017
second.name=Krishnan
full.name=Dinesh Krishnan
first.name=Dinesh
country=India
References
1. Java Properties Class
2. Java File Class
3. Java FileOutputStream Class
More from my site

Hello, folks, I am a founder of idineshkrishnan.com. I love open source technologies, If you find my tutorials are useful, please consider making donations to these charities.
No responses yet