How to read a Java Properties file

Java Properties file FAQ: Can you show me how to read a Java Properties file?

Solution

Sure. Here’s a snippet of Java source code that shows how to read a Java Properties file named Pizza.properties. It reads in two properties named CRUST and TOPPINGS.:

Properties properties = new Properties();
FileInputStream in = new FileInputStream("Pizza.properties");
properties.load(in);
in.close();

String crust = properties.getProperty("CRUST");
String toppings = properties.getProperty("TOPPINGS");

This simple coding approach makes using Java Properties files very convenient to use for storing your application properties.

What a properties file looks like

A Java Properties file has a collection of name/value pairs, separated by an equal sign, and comments allowed, like this:

# the user's name
username=al

# the user's zip code
zip_code=99676