Properties file is being used commonly in every project to get external or supported parameters like configurations, urls, port numbers and ids etc. We can handle these files using file stream APIs like FileInputStream, FileOutputStream etc, provided in java by treating these files as normal text files. But another way is also there to handle the property files, which is the best and efficient way to handle these by using the API java.util.Properties.
The class Properties has few methods on which the below are very important and most useful.
Create a properties file which contains keys separated by new line and their corresponding value pairs.
Below is a sample properties file which has three properties “name”, “message.ok” and “message.error”.
Test.properties
name=learningMela
message.ok=You are a valid user
message.error=You are not valid user
Let us write a java program to use these properties.
Below is a sample program which reads the property “name” from the property file and if the comparison is true then it will print the value of the property “message.ok” else prints the value of the key “message.error”.
Learning Mela, author: Anand Y
Java code: PropertiesTest.javaimport java.util.Properties;
public PropertiesTest
{
String propFile="Test.properties";
public static void main(String[] arg)
{
Properties props = new Properties();
props.load(this.getClass().getResourceAsStream("/Test.properties"));
if("learningMela".equals(props.getProperty("name")))
System.out.println(props.getProperty("message.ok"));
else
System.out.println(props.getProperty("message.error"));
}
}
The below statement is to load the property file from the path where the .class file of the above program is existed.
props.load(this.getClass().getResourceAsStream("/Test.properties"));
Visit articles page of the website learningmela.com for more details.
The class Properties has few methods on which the below are very important and most useful.
- load(InputStream in): To load the external properties file
- loadFromXML(InputStream in): To load an XML file as a property file
- getProperty(String key): To read value of a property key from the property file
- getProperty(String key,String defaultValue): To read value of a property key from the property file. If the key doesn’t exist then it will return the defaultValue.
Create a properties file which contains keys separated by new line and their corresponding value pairs.
Below is a sample properties file which has three properties “name”, “message.ok” and “message.error”.
Test.properties
name=learningMela
message.ok=You are a valid user
message.error=You are not valid user
Let us write a java program to use these properties.
Below is a sample program which reads the property “name” from the property file and if the comparison is true then it will print the value of the property “message.ok” else prints the value of the key “message.error”.
Learning Mela, author: Anand Y
Java code: PropertiesTest.javaimport java.util.Properties;
public PropertiesTest
{
String propFile="Test.properties";
public static void main(String[] arg)
{
Properties props = new Properties();
props.load(this.getClass().getResourceAsStream("/Test.properties"));
if("learningMela".equals(props.getProperty("name")))
System.out.println(props.getProperty("message.ok"));
else
System.out.println(props.getProperty("message.error"));
}
}
The below statement is to load the property file from the path where the .class file of the above program is existed.
props.load(this.getClass().getResourceAsStream("/Test.properties"));
Visit articles page of the website learningmela.com for more details.