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

No comments:

Post a Comment