In this tutorial, How to read and write a properties file content in Java
Java properties file reader example
In this example, you will learn how to read a key and its values from a properties file and display it to the console
Let’s declare the properties file
database=mysql
hostname=localhost
username=john
password=
Here is an example and step by sep
Create a properties object, Properties is a data structure to store keys and values in Java.
Create URL object using
ClassLoader
.getSystemResource
methodCreate a
InputStream
usingurl.openStream
methodLoad the properties content into the java properties object using the
load
methodAdd
try
andcatch
block forIOExceptions
andFIleNotFoundException
One way is, Print the value using
getProperty
with the key of a property objectAnother way is to iterate properties object for the loop
First, get all keys using
stringPropertyNames
using for loop print the key and values.
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Properties;
import java.util.Set;
public class PropertiesReader {
public static void main(String[] args) {
Properties properties = new Properties();
java.net.URL url = ClassLoader.getSystemResource("database.properties");
try {
properties.load(url.openStream());
} catch (FileNotFoundException fie) {
fie.printStackTrace();
}
catch (IOException e) {
e.printStackTrace();
}
System.out.println(properties.getProperty("hostname"));
Set<String> keys = properties.stringPropertyNames();
for (String key : keys) {
System.out.println(key + " - " + properties.getProperty(key));
}
}
}
Output:
localhost
hostname - localhost
password -
database - mysql
username - john
How to read a properties file line by line in Java
These examples read properties in Java with line-by-line
- created a
File
object with an absolute path - Create
BufferedReader
usingFileReader
object - get the First line of the properties file using
readLine
ofBufferedReader
- Loop using a while loop until the end of the line reached
- Print each line
import java.io.*;
public class PropertiesLineReader {
public static void main(String[] args) {
BufferedReader br = null;
File file = new File("B:\\database.properties");
try {
br = new BufferedReader(new FileReader(file));
String line = br.readLine();
while (line != null) {
line = br.readLine();
System.out.println(line);
}
} catch (FileNotFoundException e1) {
e1.printStackTrace();
}
catch (IOException e) {
e.printStackTrace();
} finally {
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
How to write a key and values to a properties file in Java
In this example, You can read and write a property using
- First, create a
File
object - Create a writer object using
FileWriter
- Create properties object and add new properties or update existing properties if the properties file exists
setProperties
method do update or add key and valuesstore
method of theproperties
object writes to the properties file, You have to add the comment which appends to the properties file as a comment
Here is a complete example of reading and writing a properties file
import java.io.*;
import java.util.Properties;
public class PropertieWriter {
public static void main(String[] args) {
FileReader reader = null;
FileWriter writer = null;
File file = new File("B:\\database.properties");
try {
reader = new FileReader(file);
writer = new FileWriter(file);
Properties p = new Properties();
p.load(reader);
p.setProperty("name","john");
p.store(writer,"write a file");
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
The output of a file
#write a file
#Fri Aug 27 21:52:38 IST 2021
name=john
Conclusion
You learned to read a properties file with keys and values as well as line-by-line and also write keys and values to the properties file