Java Properties File: How to Read Values in Java?

Last updated
App Shah
Crunchify » Java and J2EE Tutorials » Java Properties File: How to Read Values in Java?

.properties is a file extension for files mainly used in Java related technologies to store the configurable parameters of an application. They can also be used for storing strings for Internationalization and localization; these are known as Property Resource Bundles.

Each parameter is stored as a pair of strings, one storing the name of the parameter (called the key/map), and the other storing the value.

Below is a sample Java program which demonstrate you how to retrieve/read values in Java. For update follow this tutorial.

Another must readRead value using Spring “singleton” Scope in your Java Enterprise Application

We will create 3 files:

  3. file

Main Class ( which will call getPropValues() method from class

Let’s get started:

Step-1: Create file.

  1. Create Folder “resources” under Java Resources folder if your project doesn’t have it.
  2. create file with below value.

How to read in Java - Crunchify Tips

/Java Resources/ file content:

#Crunchify Properties


Create file



 * @author

public class CrunchifyReadConfigMain {

	public static void main(String[] args) throws IOException {
		CrunchifyGetPropertyValues properties = new CrunchifyGetPropertyValues();


Create file


import java.util.Date;
import java.util.Properties;

 * @author

public class CrunchifyGetPropertyValues {
	String result = "";
	InputStream inputStream;

	public String getPropValues() throws IOException {

		try {
			Properties prop = new Properties();
			String propFileName = "";

			inputStream = getClass().getClassLoader().getResourceAsStream(propFileName);

			if (inputStream != null) {
			} else {
				throw new FileNotFoundException("property file '" + propFileName + "' not found in the classpath");

			Date time = new Date(System.currentTimeMillis());

			// get the property value and print it out
			String user = prop.getProperty("user");
			String company1 = prop.getProperty("company1");
			String company2 = prop.getProperty("company2");
			String company3 = prop.getProperty("company3");

			result = "Company List = " + company1 + ", " + company2 + ", " + company3;
			System.out.println(result + "\nProgram Ran on " + time + " by user=" + user);
		} catch (Exception e) {
			System.out.println("Exception: " + e);
		} finally {
		return result;


Run CrunchifyReadConfigMain and checkout result.

Company List = Google, eBay, Yahoo
Program Ran on Mon May 13 21:54:55 PDT 2013 by user=Crunchify

As usually happy coding and enjoy..!! Do let me know if you see any exception. List of all Java Tutorials.

Are you running above program in IntelliJ IDE and getting NullPointerException?

Please follow below tutorial for fix.

How to add Resources Folder, Properties at Runtime into IntelliJ classpath? Adding Property files to Classpath

34 thoughts on “Java Properties File: How to Read Values in Java?”

  1. Hey Dude,

    how can i use the strings which was created in CrunchifyGetPropertyValues also in I have no clue, how i can have access to it.


  2. On load I am getting compile time error “The method is undefined for the type properties” and telling to cast it but after casting ti I am still getting compile time error

  3. Nice post.. it helped me alot but I don’t want to use new CrunchifyGetPropertyValues(); every time. I want to load the property file only once and want to use it. and
    getPropValues() will be static . right now I am not able to do so can anyone please help me

  4. hii, how can i write database connection parameter like(driver class,username,password) from multiple database like oracle and mysql in a same properties file

  5. Hello, This work perfect. but I have a problem trying to do an “while” I had my “prop.load(String)” inside in my “while” and only print the last item in the query. Any help to modify or print the loop?

  6. What if you wanted a different property file for each server that it’s deployed on. For example I have a dev/qa folder with the properties file in it. How do I tell it “hey im on this server” by using this load properties method?

  7. Hi,

    I tried the code, I am getting the below exception…can you pls guide?

    Exception in thread "main" java.lang.NullPointerException
    	at java.util.Properties$LineReader.readLine(Unknown Source)
    	at java.util.Properties.load0(Unknown Source)
    	at java.util.Properties.load(Unknown Source)
    	at com.crunchify.tutorials.CrunchifyGetPropertyValues.getPropValues(
    	at com.crunchify.tutorials.CrunchifyReadConfigMain.main(

Leave a Comment