In this tutorial, You will Learn
- How to read properties file using configparser in python
- Parse Properties file using JProperties library in phyton
- Write to a Properties file in Python
How to read properties in Python with examples?
In this example, you will learn how to Read the key and its values from a properties file and display it to the console
Let’s declare the properties file
database=ostgress
hostname=localhost
username=john
password=
Here is an example and step by step.
Create a properties object, Properties is a data structure to store keys and values in Java
Create URL object using
ClassLoader
.getSystemResource
methodCreate an
InputStream
usingurl.openStream
methodLoad the properties content into the java properties object using
load
methodAdd
try
andcatch
blocks forIOExceptions
andFIleNotFoundException
Oneway, Print the value using getProperty with the key of a properties object
Another way is to iterate properties object for 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 properties file line by line in Java
This example 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 properties using
- First, create a
File
object - Create a writer object using
FileWriter
- Create properties object and add new properties or update existing properties if properties file exists
- The
setProperties
method do update or add key and values store
method of theproperties
object writes to the properties file, You have to add the comment which is appended 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();
}
}
}
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