Reading and writing configuration for Java application using Properties class
- Create a Propertiesobject by using empty constructor:
Properties props = new Properties();
- Create a Properties object by supplying a default properties list:
Properties defaultProps = new Properties(); // set default properties
Properties props = new Properties(defaultProps); / create main Properties object
- Load properties from a .properties file using a FileReader object
File configFile = new File("config.properties");
FileReader reader = new FileReader(configFile);
Properties props = new Properties();
props.load(reader); // load the properties file:
- Load properties from a plain text file using an InputStream object:
File configFile = new File("config.properties");
InputStream inputStream = new FileInputStream(configFile);
Properties props = new Properties();
props.load(inputStream);
- The Properties class has two methods for retrieving value of a property in the properties file:
- String getProperty(String key): returns value of the property specified by the given key. It returns null if the key not found.
- String getProperty(String key, String defaultValue): like the above method, but this method will return a default value if the key not found.
String host = props.getProperty("host");
- Setting value for a specific property is pretty simple, using this sole method:
- Object setProperty(String key, String value)
- This method returns previous value of the property specified by the given key.
props.setProperty("host", "www.sevdanurgenc.com");
- Save to plain text file using a Writerobject:
File configFile = new File("config.properties");
FileWriter writer = new FileWriter(configFile);
props.store(writer, "host settings");