Java Property File

This section contains the detail about property file, it's use and how can we read it via our Java code.

Java Property File

Java Property File

This section contains the detail about property file, it's use and how can we read it via our Java code.


What is property file ?

A property file is used to store an application's configuration parameters. Except the configuration parameter storage, it is also used for internationalization and localization for storing strings written in a different languages.

Property file store pair of string as key-value pair. Key store the name of the parameter and value stores the value. Generally, each line in the property file is used to store one property.

File Format

Each line of property file can be written in following format :

  • key=value
  • key = value
  • key: value
  • key value

Here is the video tutorial of: "What is properties file in Java?"

For comment, we use # and ! sign in front or start of the line. See Example below  :

# This is a comment
!This is also a comment

If your value of key is large and you need next line to continue, you can do it using back slash as given below.

# This is a comment
message = Welcome to \

If your key contains spaces, you can write it as given below :

# Key with spaces
key\ containing\ spaces = This key will be read as "Key containing spaces".

The # and ! sign is used as comment but when it is part of the a key's value it has no effect on the key, see below. Also you can write Unicode as provided below:

#No effect on back slash
website =
# Unicode
tab : \u0009

Reading File using Java Code

You can read the key-value pair string of property file using Java code as given below :

The property file is given below :

#This is a Sample Property File
Name = Roseindia
Portal =
Message = Welcome To Roseindia !!!

The Java code to read this property file is given below :

import java.util.*;

public class ReadPropertyFile {
public static void main(String args[]) {
try {
Properties pro = new Properties();
FileInputStream in = new FileInputStream("C:/");
// getting values from property file
String name = pro.getProperty("Name");
String portal = pro.getProperty("Portal");
String message = pro.getProperty("Message");

// Printing Values fetched

} catch (IOException e) {
System.out.println("Error is:" + e.getMessage());




Welcome To Roseindia !!!

Download Source Code