Java Programs›File Handling›Read a Properties File

Read a Properties File in Java

beginner·  File Handling  ·  File I/O

Problem

A .properties file stores configuration as key=value lines, with # starting a comment; java.util.Properties reads and writes this format.

Load an application's name, port and feature flag from a properties file, using a default for a setting that's missing.

Input
app.name=DevStack Demo, app.port=8080, feature.dark=true
Output
Port: 8080, Timeout: 30 (default)

Java Program

Java
import java.io.IOException; import java.io.Reader; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; import java.util.Properties; public class ReadPropertiesFile { public static void main(String[] args) throws IOException { Path file = Paths.get("app.properties"); Files.writeString(file, "# application settings\napp.name=DevStack Demo\napp.port=8080\nfeature.dark=true\n"); Properties props = new Properties(); try (Reader reader = Files.newBufferedReader(file)) { props.load(reader); // parses key=value lines and skips # comments } String name = props.getProperty("app.name"); int port = Integer.parseInt(props.getProperty("app.port")); boolean dark = Boolean.parseBoolean(props.getProperty("feature.dark")); String timeout = props.getProperty("app.timeout", "30"); // default when the key is missing System.out.println("Name: " + name); System.out.println("Port: " + port); System.out.println("Dark mode: " + dark); System.out.println("Timeout: " + timeout + " (default)"); Files.delete(file); } }

Output

Name: DevStack Demo Port: 8080 Dark mode: true Timeout: 30 (default)

Core Logic

Properties.load parses the whole file into string pairs, and the program converts each value to the type it needs.

How It Works
  1. 1The program writes a sample app.properties containing a comment and three settings.
  2. 2props.load(reader) parses every key=value line and skips # comments.
  3. 3getProperty returns strings, which are converted with Integer.parseInt and Boolean.parseBoolean.
  4. 4getProperty("app.timeout", "30") returns the default because the key is missing.
The missing timeout falls back to 30 without any null check.
💡

Key Point: All values come back as text — forgetting to convert "8080" to an int is a common source of bugs.

Key Concepts

java.util.Propertiesload()getProperty(key, default)

Approach 2: Writing a Properties File

Java
import java.io.IOException; import java.io.Reader; import java.io.Writer; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; import java.util.Properties; import java.util.TreeSet; public class WritePropertiesFile { public static void main(String[] args) throws IOException { Path file = Paths.get("user.properties"); Properties out = new Properties(); out.setProperty("theme", "dark"); out.setProperty("language", "en"); out.setProperty("fontSize", "14"); try (Writer w = Files.newBufferedWriter(file)) { out.store(w, "User preferences"); // writes a comment line, a timestamp line, then key=value pairs } Properties in = new Properties(); try (Reader r = Files.newBufferedReader(file)) { in.load(r); } for (String key : new TreeSet<>(in.stringPropertyNames())) { // sorted for stable output System.out.println(key + " = " + in.getProperty(key)); } Files.delete(file); } }

Output

fontSize = 14 language = en theme = dark

Core Logic

store writes the settings back out in the same format, with a comment header, so they can be reloaded later.

How It Works
  1. 1setProperty adds three user preferences.
  2. 2store(writer, "User preferences") writes a comment line, a timestamp and the key=value pairs.
  3. 3The file is loaded again, and the keys are printed in sorted order.
The reloaded values match what was stored: theme, language and font size.
💡

Key Point: Properties doesn't preserve key order, so sort the keys when printing or comparing.

Key Concepts

setProperty()store()stringPropertyNames()

Interview Questions

How do you load a properties file from the classpath?

Use getClass().getResourceAsStream("/app.properties"), which works inside a JAR where there's no ordinary file path.

Which encoding do properties files use?

load(InputStream) assumes ISO-8859-1, while load(Reader) uses the reader's encoding. Use a UTF-8 reader for non-English text.

Learn the Concept

Related Programs