Sample program to read and display data from properties file


In this article we are going to discuss about usage of properties file in project.  In modern project development we need to configure so many properties. Like when we try to connect with Database we need to configure Database server properties. For example user name, password and host.

Here I am discussing about sample program which is used to retrieve data from properties file.
Filename: sample.properties
## Author information configuration

com.becbe.sample.author_name=becbe
com.becbe.sample.website_url=http://www.becbe.com


Filename: ReadProperties.java
package com.becbe.sample;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

public class ReadProperties {

public static void main(String s[]) throws IOException 
{
 ReadProperties readprops=new ReadProperties();
 InputStream in=readprops.getClass().getClassLoader().getResourceAsStream("sample.properties");
 Properties properties=new Properties();
 properties.load(in);
 
 System.out.println("========================================");
 System.out.println("Author Name: "+properties.getProperty("com.becbe.sample.author_name"));
 System.out.println("Website URL: "+properties.getProperty("com.becbe.sample.website_url"));
 System.out.println("========================================");
}
 
}

Output:

========================================
 Author Name: becbe
Website URL: http://www.becbe.com
========================================

Tags: java, properties,sample
SHARE

    Blogger Comment
    Facebook Comment

0 comments :

Post a Comment