Read a Properties File in Java
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.
Java Program
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
Core Logic
Properties.load parses the whole file into string pairs, and the program converts each value to the type it needs.
- 1The program writes a sample
app.propertiescontaining a comment and three settings. - 2
props.load(reader)parses everykey=valueline and skips#comments. - 3
getPropertyreturns strings, which are converted withInteger.parseIntandBoolean.parseBoolean. - 4
getProperty("app.timeout", "30")returns the default because the key is missing.
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
Approach 2: Writing a Properties File
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
Core Logic
store writes the settings back out in the same format, with a comment header, so they can be reloaded later.
- 1
setPropertyadds three user preferences. - 2
store(writer, "User preferences")writes a comment line, a timestamp and the key=value pairs. - 3The file is loaded again, and the keys are printed in sorted order.
Key Point: Properties doesn't preserve key order, so sort the keys when printing or comparing.
Key Concepts
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.