Create and write to the properties file using java code.

Below java code snippet will create a new property file and write the properties into it.

import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;

public class WriteProperty {
   
    public static void main(String[] args) {
        Properties property = new Properties();
        try {
            // set the properties values
            property.setProperty("url", "host IP Address");
            property.setProperty("username", "Amzi");
            property.setProperty("password", "passw0rd");

            // save properties to project root folder
            property.store(new FileOutputStream("config.properties"), null);
            System.out
            .print("properties are saved into the destination folder");

        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}


Output:

properties are saved into the destination folder

config.properties file should look something like this:

#Fri Aug 23 11:49:29 EDT 2013
password=passw0rd
url=host IP Address
username=Amzi

1 comment:

  1. Thanks for the post, I am techno savvy. I believe you hit the nail right on the head. I am highly impressed with your blog. It is very nicely explained. Your article adds best knowledge to our Java Online Training from India. or learn thru Java Online Training from India Students.

    or learn thru JavaScript Online Training from India. Appreciating the persistence you put into your blog and detailed information you provide. Kindly keep blogging.

    ReplyDelete