Skip to main content

Using Properties in Java

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.

  1. load(InputStream in): To load the external properties file
  2. loadFromXML(InputStream in): To load an XML file as a property file
  3. getProperty(String key): To read value of a property key from the property file
  4. 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.
Let us see an example to understand how to use the Properties class.
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.

Popular posts from this blog

Fun Facts

Do you want to improve your knowledge? Do you want to impress friends with some facts? Then Fun Facts app is for you. "Fun Facts" is an android app shows some quotes which are funny but real facts. Install the app @  https://play.google.com/store/apps/details?id=com.sonuappz.funfacts

Java Program to use LDAP server

The below is a java program to demonstrate how to use the LDAP server connection to validate a user authentication. To check the authentication using LDAP server we required the user id and its password. We need to create a context to the LDAP server before connecting to it. And also we required to know the field names in the LDAP server.   Click here   to know more about the project.    Visit  Projects  page of  learningmela.com  to download more projects.

Java program to create PDF files

The below is a program to create a PDF text file in Java language.   This program reads a line of text from user and the text will be written to a pdf file. The pdf file name also be taken from the user input. This is a console java program.   Click here   to know more about the project.    Visit  Projects  page of  learningmela.com  to download more projects.