1. Overview
In this tutorial, We'll be learning how to read from and write to the properties file in java.
Java API priovides a class Properties which is part of java.util package.
The Properties class stores the set of key, value pairs as properties. It internally stores all properties in a Stream. The Properties can be saved to a stream or loaded from a stream. Each key and its corresponding value in the property list stored as a string.
public class Properties extends Hashtable<Object,Object>
Properties inherit from Hashtable, the put and putAll methods can be applied to a Properties object. Their use is strongly discouraged as they allow the caller to insert entries whose keys or values are not Strings. The setProperty method should be used instead.
2. Setting and Getting values from Properties
Properties class provides methods to set and get property using setProperty() and getProperty() methods.
Syntax:
public Object setProperty(String key, String value) public String getProperty(String key)
The above methods take only String values as a key-value pair. No other types are allowed.
Example:
package com.java.w3schools.blog.java.properties; import java.util.Properties; public class PropertiesSetGetExample { public static void main(String[] args) { Properties properties = new Properties(); properties.setProperty("IN", "India"); properties.setProperty("USA", "United States"); properties.setProperty("AUS", "Australia"); System.out.println("All properties : " + properties); System.out.println("Retrieving the properties by key "); String AUSValue = properties.getProperty("AUS"); System.out.println("Value for AUS key is " + AUSValue); String INValue1 = properties.getProperty("IN"); System.out.println("Value for IN key is " + INValue1); String USAValue1 = properties.getProperty("USA"); System.out.println("Value for USA key is " + USAValue1); } }
Output:
All properties : {USA=United States, IN=India, AUS=Australia} Retrieving the properties by key Value for AUS key is Australia Value for IN key is India Value for USA key is United States
3. Writing into Properties File
Properties API has a very useful method to construct a Properties file in java.
public void store(OutputStream out, String comments) throws IOException
We need to pass the file location where the file should be created and comments to the file. Providing comments value is optional. If we do not want to pass the comments then provide null.
package com.java.w3schools.blog.java.properties; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.OutputStream; import java.util.Properties; public class WritingToPropertyFile { public static void main(String[] args) { Properties properties = new Properties(); properties.setProperty("country.india", "Delhi"); properties.setProperty("country.australia", "Canberra"); properties.setProperty("country.usa", "Washington"); properties.setProperty("country.uae", "Abu Dhabi"); properties.setProperty("country.newzealand", "Wellington"); System.out.println("Writing the properties into a file"); try (OutputStream output = new FileOutputStream("files/application.properties")) { properties.store(output, "This is Country-Capital's property file"); System.out.println("Data stored into file"); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } }
Generated Properties file:
#This is Country-Capital's property file #Thu Aug 22 22:03:22 IST 2019 country.india=Delhi country.usa=Washington country.newzealand=Wellington country.australia=Canberra country.uae=Abu Dhabi
Added comments and file created date time to the property file.
4. Loading from a property file
Properties have a load() method that takes a file location.
Syntax:
public void load(InputStream inStream) throws IOException
Property file to be loaded:
welcome.message=Hello Mate, Welcome to Java8Example blog payment.creditcard=Enter Credit Card Number payment.netbanking=Select Your Bank payment.debitcard=Enter Debit Card Number payment.paypal=Enter PayPal Id
Example:
package com.java.w3schools.blog.java.properties; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStream; import java.util.Properties; public class LoadingFromFile { public static void main(String[] args) { Properties properties = new Properties(); System.out.println("Writing the properties into a file"); try (InputStream input = new FileInputStream("files/payment.properties")) { properties.load(input); System.out.println("payment.properties loaded"); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } System.out.println(); String welcomeMsg = properties.getProperty("welcome.message"); System.out.println("welcome.message value : " + welcomeMsg); String netBanking = properties.getProperty("payment.netbanking"); System.out.println("payment.netbanking value : " + netBanking); String paypal = properties.getProperty("payment.paypal"); System.out.println("payment.paypal value : " + paypal); } }
Output:
Writing the properties into a file payment.properties loaded welcome.message value : Hello Mate, Welcome to Java8Example blog payment.netbanking value : Select Your Bank payment.paypal value : Enter PayPal Id
5. Reading/Loading the property file from classpath
Loading the file from the classpath is the same as above. But, Referring to the file location is different.
Just use the following line instread of new FileInputStream("files/payment.properties").
try (InputStream input = LoadingFromFile.class.getClassLoader().getResourceAsStream("payment..properties"))
No comments:
Post a Comment