How to read a Java Properties file

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

Here is 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

Also, if it helps to know it, a 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

Share it!

There’s just one person behind this website; if this article was helpful (or interesting), I’d appreciate it if you’d share it. Thanks, Al.

Add new comment

The content of this field is kept private and will not be shown publicly.

Anonymous format

  • Allowed HTML tags: <em> <strong> <cite> <code> <ul type> <ol start type> <li> <pre>
  • Lines and paragraphs break automatically.
By submitting this form, you accept the Mollom privacy policy.