Steps to Read/write Property File in java

Steps to Read/write Property File in java

Create a Java project and create a “config.properties” file under the root folder (project) not under src.

Project structure:

property file project structure

Config.properties:

config properties file content

Reading Property File:

Create a class file “PropertyFileRead” and paste the below code,

package com.ngdeveloper.com;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Properties;

public class PropertyFileRead {

	public static void main(String[] args) {
		Properties propFile = new Properties();
		try {
			// Loading property file
			propFile.load(new FileInputStream("config.properties"));

			// reading the value from the property file
			String welcomeUser = propFile.get("welcome").toString();
			System.out.println(welcomeUser);

			// Object welcomeUser = propFile.get("welcome");
			// System.out.println(welcomeUser.toString());

		} catch (FileNotFoundException fnfe) {
			System.out.println("File Not Found : " + fnfe.getMessage());
		} catch (IOException ioe) {
			System.out.println(ioe.getMessage());
		}

	}
}

Output:
welcome to Javadomain

Writing to Property File:

Create a class file “PropertyFileWrite and paste the below code,

1. Using setProperty method [it calls put method internally]

package com.ngdeveloper.com;

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

public class propertyFileWrite {

	public static void main(String[] args) {
		Properties propFile = new Properties();

		try {
			// Loading property file
			propFile.load(new FileInputStream("config.properties"));

			// Setting key and value
			propFile.setProperty("javadomain_url", "www.ngdeveloper.com");

			// Storing the key values
			propFile.store(new FileOutputStream("config.properties"),
					"using setproperty method");

			System.out.println(propFile.get("javadomain_url").toString()
					+ " written successfully in config.properties file");

		} catch (FileNotFoundException fnfe) {
			System.out.println("File Not Found : " + fnfe.getMessage());
		} catch (IOException ioe) {
			System.out.println(ioe.getMessage());
		}
	}

}

Output:
www.ngdeveloper.com written successfully in config.properties file

config.properties file – Before the program execution,

config properties file content

config.properties – After the program execution,

config properties fie after program execution

2. Using put method:

package com.ngdeveloper.com;

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

public class propertyFileWrite {

	public static void main(String[] args) {
		Properties propFile = new Properties();

		try {
			// Loading property file
			propFile.load(new FileInputStream("config.properties"));

			// Setting key and value

			// propFile.setProperty("javadomain_url", "www.ngdeveloper.com");
			propFile.put("javadomain", "Javadomain for java programmers");

			// Storing the key values
			/*
			 * propFile.store(new FileOutputStream("config.properties"),
			 * "using setproperty method");
			 */
			propFile.store(new FileOutputStream("config.properties"),
					"using put method");

			System.out.println(propFile.get("javadomain").toString()
					+ " written successfully in config.properties file");

		} catch (FileNotFoundException fnfe) {
			System.out.println("File Not Found : " + fnfe.getMessage());
		} catch (IOException ioe) {
			System.out.println(ioe.getMessage());
		}
	}

}

Output:
Javadomain for java programmers written successfully in config.properties file

config.properties file – before the program execution,

config properties fie after program execution

config.properties file – after the program execution,
config properties file after program execution

 

Download the source code:

PropertyFileReadWrite

 

Recommended Java Books :

578 total views, 2 views today

Leave a Reply

Your email address will not be published. Required fields are marked *