In the post, we show how to read/write Properties object from/to Properties File (.properties file).
Contents
I. Java – Write Properties object to Properties File
1. Properties store() method
We use java.util.Properties.store()
methods:
// 1. public void store(OutputStream out, String comments) throws IOException -> Writes this property list (key and element pairs) in this Properties table to the output stream in a format suitable for loading into a Properties table using the load(InputStream) method. // 2. public void store(Writer writer, String comments) throws IOException -> Writes this property list (key and element pairs) in this Properties table to the output character stream in a format suitable for using the load(Reader) method. |
2. Program – write Properties to File
package com.javasampleapproach.propertiesfile; import java.io.FileOutputStream; import java.io.FileWriter; import java.io.IOException; import java.io.OutputStream; import java.io.Writer; import java.util.Properties; public class JavaWriteProperties2File { public static void main(String[] args) { Properties properties = new Properties(); properties.put("db.username", "username"); properties.put("db.password", "password"); properties.put("db.driver", "org.postgresql.Driver"); properties.put("db.url", "jdbc:postgresql://localhost/testdb"); /* * Approach 1: * use -> 'java.util.Properties.store(OutputStream out, String comments) throws IOException' */ String propertiesFile = System.getProperty("user.dir") + "\\file.properties"; try(OutputStream propertiesFileWriter = new FileOutputStream(propertiesFile)){ properties.store(propertiesFileWriter, "save to properties file"); }catch(IOException ioe){ ioe.printStackTrace(); } /* * Approach 2: * use -> 'java.util.Properties.store(Writer writer, String comments) throws IOException' */ propertiesFile = System.getProperty("user.dir") + "\\file_1.properties"; try(Writer propertiesFileWriter = new FileWriter(propertiesFile)){ properties.store(propertiesFileWriter, "save to properties file"); }catch(IOException ioe){ ioe.printStackTrace(); } } } |
-> .properties
output file:
#save to properties file #Tue Mar 06 21:31:28 ICT 2018 db.password=password db.url=jdbc\:postgresql\://localhost/testdb db.username=username db.driver=org.postgresql.Driver |
II. Java – Read Properties File to Properties object
1. Properties load() method
We use java.util.Properties.load()
methods:
// 1. public void load(InputStream inStream) throws IOException -> Reads a property list (key and element pairs) from the input byte stream. The input stream is in a simple line-oriented format as specified in load(Reader) and is assumed to use the ISO 8859-1 character encoding; that is each byte is one Latin1 character. // 2. public void load(Reader reader) throws IOException -> Reads a property list (key and element pairs) from the input character stream in a simple line-oriented format. |
2. Program – Read from Properties File
package com.javasampleapproach.propertiesfile; import java.io.FileInputStream; import java.io.FileReader; import java.io.IOException; import java.io.InputStream; import java.io.Reader; import java.util.Properties; public class JavaReadFile2Properties { public static void main(String[] args) { Properties properties = new Properties(); String propertiesFile = System.getProperty("user.dir") + "\\file.properties"; /* * Approach 1: * -> use 'java.util.Properties.load(InputStream inStream) throws IOException' */ try(InputStream inputStream = new FileInputStream(propertiesFile)){ properties.load(inputStream); }catch(IOException ioe) { ioe.printStackTrace(); } properties.forEach((k, v) -> System.out.println(String.format("key = %s, value = %s", k, v))); /* key = db.driver, value = org.postgresql.Driver key = db.username, value = username key = db.url, value = jdbc:postgresql://localhost/testdb key = db.password, value = password */ System.out.println("----------------------------"); /* * Approach 2: * -> 'use java.util.Properties.load(Reader reader) throws IOException' */ try(Reader reader = new FileReader(propertiesFile)){ properties.load(reader); }catch(IOException ioe) { ioe.printStackTrace(); } properties.forEach((k, v) -> System.out.println(String.format("key = %s, value = %s", k, v))); /* key = db.driver, value = org.postgresql.Driver key = db.username, value = username key = db.url, value = jdbc:postgresql://localhost/testdb key = db.password, value = password */ } } |