Normally, Java properties file is used to store project configuration data or settings. In this tutorial, we will show you how to read and write to/from a .properties file.
Properties prop = new Properties();
// set key and value
prop.setProperty("db.url", "localhost");
prop.setProperty("db.user", "favtuts");
prop.setProperty("db.password", "password");
// save a properties file
prop.store(outputStream, "");
// load a properties file
prop.load(inputStream)
// get value by key
prop.getProperty("db.url");
prop.getProperty("db.user");
prop.getProperty("db.password");
// get all keys
prop.keySet();
// print everything
prop.forEach((k, v) -> System.out.println("Key : " + k + ", Value : " + v));
A simple Maven project structure for testing.

1. Write to the properties file
Set the property key and value, and save it somewhere.
PropertiesFileExamples.java
package com.favtuts.io.howto;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.nio.file.Paths;
import java.util.Properties;
public class PropertiesFileExamples {
public static void main(String[] args) {
String userDirectory = Paths.get("")
.toAbsolutePath()
.toString();
// path/to/config.properties
String configFilePath = userDirectory + "/java-io/src/main/resources/config.properties";
System.out.println(configFilePath);
// Write to the properties file
writePropertiesFile(configFilePath);
}
private static void writePropertiesFile(String configFilePath) {
try (OutputStream output = new FileOutputStream(configFilePath)) {
Properties prop = new Properties();
// set the properties value
prop.setProperty("db.url", "localhost");
prop.setProperty("db.user", "favtuts");
prop.setProperty("db.password", "password");
// save properties to project root folder
prop.store(output, null);
System.out.println(prop);
} catch (IOException e) {
e.printStackTrace();
}
}
}
Output
{db.user=favtuts, db.password=password, db.url=localhost}
The path/to/config.properties is created.
path/to/config.properties
#Sat May 14 18:00:34 ICT 2022
db.user=favtuts
db.password=password
db.url=localhost
2. Load a properties file
Load a properties file from the file system and retrieved the property value.
PropertiesFileExamples.java
package com.favtuts.io.howto;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Paths;
import java.util.Properties;
public class PropertiesFileExamples {
public static void main(String[] args) {
String userDirectory = Paths.get("")
.toAbsolutePath()
.toString();
// path/to/config.properties
String configFilePath = userDirectory + "/java-io/src/main/resources/config.properties";
System.out.println(configFilePath);
// Load a properties file
loadPropertiesFile(configFilePath);
}
private static void loadPropertiesFile(String configFilePath) {
try (InputStream input = new FileInputStream(configFilePath)) {
Properties prop = new Properties();
// load a properties file
prop.load(input);
// get the property value and print it out
System.out.println(prop.getProperty("db.url"));
System.out.println(prop.getProperty("db.user"));
System.out.println(prop.getProperty("db.password"));
} catch (IOException e) {
e.printStackTrace();
}
}
}
Output
localhost
favtuts
password
3. Load a properties file from classpath
Load a properties file config.properties from project classpath, and retrieved the property value.
src/main/resources/config.properties
db.user=favtuts
db.password=password
db.url=localhost
PropertiesFileExamples.java
package com.favtuts.io.howto;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Paths;
import java.util.Properties;
public class PropertiesFileExamples {
public static void main(String[] args) {
// Load a properties file from classpath
loadPropertiesFileClassPath("config.properties");
}
private static void loadPropertiesFileClassPath(String resourceFileName) {
try (InputStream input = PropertiesFileExamples.class.getClassLoader().getResourceAsStream(resourceFileName)) {
Properties prop = new Properties();
if (input == null) {
System.out.println("Sorry, unable to find " + resourceFileName);
return;
}
//load a properties file from class path, inside static method
prop.load(input);
//get the property value and print it out
System.out.println(prop.getProperty("db.url"));
System.out.println(prop.getProperty("db.user"));
System.out.println(prop.getProperty("db.password"));
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
Output
db.user=favtuts
db.password=password
db.url=localhost
4. Prints everything from a properties file
Load a properties file config.properties from project classpath, and print out the keys and values.
PropertiesFileExamples.java
package com.favtuts.io.howto;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Paths;
import java.util.Properties;
import java.util.Set;
public class PropertiesFileExamples {
public static void main(String[] args) {
// Prints everything from a properties file
PropertiesFileExamples app = new PropertiesFileExamples();
app.printAll("config.properties");
}
private void printAll(String resourceFileName) {
try (InputStream input = getClass().getClassLoader().getResourceAsStream(resourceFileName)) {
Properties prop = new Properties();
if (input == null) {
System.out.println("Sorry, unable to find " + resourceFileName);
return;
}
prop.load(input);
// Java 8 , print key and values
prop.forEach((key, value) -> System.out.println("Key : " + key + ", Value : " + value));
// Get all keys
prop.keySet().forEach(x -> System.out.println(x));
Set<Object> objects = prop.keySet();
/*Enumeration e = prop.propertyNames();
while (e.hasMoreElements()) {
String key = (String) e.nextElement();
String value = prop.getProperty(key);
System.out.println("Key : " + key + ", Value : " + value);
}*/
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
Output
Key : db.user, Value : favtuts
Key : db.password, Value : password
Key : db.url, Value : localhost
db.user
db.password
db.url
Download Source Code
$ git clone https://github.com/favtuts/java-core-tutorials-examples
$ cd java-io/howto