Index:
1. Reading Property File
2. Writting Property File
We have below file structure:
-output.config
a. Java code is this.
1. Reading Property File
2. Writting Property File
We have below file structure:
PropertyFileExample
-src/main/java
-PropertyExample.java
-src/main/config
-application.config-output.config
1. Reading Property File
a. Java code is this.
Properties properties = new Properties(); InputStream inputStream = PropertyExample.class.getClassLoader()
.getResourceAsStream("application.config"); properties.load(inputStream); System.out.println("db.username: " + properties.getProperty("db.username")); System.out.println("db.password: " + properties.getProperty("db.password")); System.out.println("db.url: " + properties.getProperty("db.url"));
b. Output is:
db.username: usr db.password: 12345 db.url: kaurl
=== application.config ===== db.username=usr db.password=12345 db.url=kaurl
2. Writting to Property File
a. Java code is here.
private static void writeToPropertyFile() throws FileNotFoundException, IOException { OutputStream outputStream = new FileOutputStream("src//main//config//output.config"); Properties properties = new Properties(); properties.setProperty("lang", "english"); properties.setProperty("location", "europa"); properties.store(outputStream, "comments");
}
b. After running this code, this file will be created.
==== output.config =====
#comments #Tue Jul 10 10:00:11 EEST 2018 location=europa lang=english