Read and Write properties file in Java- Examples

.properties files are used to store information in the form of Key-Value pair in java. These are those values which we can not directly hard code into our program or some values which are user or client specific and needs to be configured based on the user environment.

For example let us say we make an application that interacts with the database and to access the database we need database name, username, password and the port no to access it which is definitely different for different user. o if our application needs to be run by multiple clients then we can not directly hard code these values and need a way to change them according to user. So in such cases we just create some properties files which contains all these vlaues and we dynamically read these values from there to perform our operations.

So lets see with examples..

How to create and write properties file in java

We can either create properties files with extension .properties manually or we can directly create them dynamically through code. So lets create and write some values to it.

WritePropertyFile.java

public class WritePropertyFile{
	
	public static void main(String[] args) {

		try (OutputStream output = new FileOutputStream("config.properties")) {

			Properties prop = new Properties();

			// set the properties value
			prop.setProperty("database", "localhost");
			prop.setProperty("username", "Codingeek");
			prop.setProperty("password", "Codingeek");

			// save properties to project root folder.
			prop.store(output, null);

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

	}
}

Output:-
#Sun Aug 24 13:34:24 IST 2014
password=Codingeek
database=localhost
username=Codingeek
  • In the above code we use try with resources so that the output stream gets closed automatically. FileOutputStream(“config.properties”) is used so as to either create a file or use the existing one with the name config.properties.
  • Then we use the Properties class to set the properties into the file.
  • This file gets stored in your project root folder else you have to specify the path if you want to store it somewhere else.

How to Read properties file in java

To read properties files we need to have the file name and we should also know about the keys of the key-value pair in the property file in order to read the respective values or you can traverse the complete list in case you don’t know the name.

In this exapmle we will use FileInputStream(file name) in order to read the resource file which lies in our project root folder.

public class ReadPropertyFile {
	public static void main(String[] args) {
		Properties prop = new Properties();

		try (InputStream input = new FileInputStream("config.properties")) {

			// load a properties file
			prop.load(input);

			// get the property value and print it out
			System.out.println("Database - " + prop.getProperty("database"));
			System.out.println("Username - " + prop.getProperty("username"));
			System.out.println("Password - " + prop.getProperty("password"));

		} catch (IOException ex) {
			ex.printStackTrace();
		}
	}
}
Output:-
Database – localhost
Username – Codingeek
Password – Codingeek

Follow this link to read how to read resources or properties files using class loader or when the files are in the source folder of your project.

Recommended -

Subscribe
Notify of
guest
1 Comment
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Borna
Borna
9 years ago

This is actually very useful, thank you for sharing the wisdom 🙂

1
0
Would love your thoughts, please comment.x
()
x
Index