Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Home Java Example Java Util Read the Key-Value of Property File in Java

Related Tutorials


 
 

Share on Google+Share on Google+

Read the Key-Value of Property File in Java

Advertisement
In this section, you will learn how to read the key-value of properties files in Java

Read the Key-Value of Property File in Java

     

In this section, you will learn how to read the key-value of properties files in Java. The properties file provides the general text editor, which have the keys and it's values.

Here, you will get the key-values of the properties files according to the key with the help of this program. Program takes the file name of the properties file and it checks, if  the file exists then it performs the next work Otherwise, it shows the appropriate messages like: " File not found! " and " Enter file name which has properties extension : ".  This messages shows until, when your entered file name is correct. If the file name is correct then  it takes the key of the properties file after that shows the appropriate key-value of the properties file. The following methods and API interfaces had been used in the program:

Properties():
This is the constructor of Properties class. It extends from the Hashtable and imports form the import java.util.* package. This constructor used to create an empty property list, which hasn't any default values. The Properties class shows the persistent set of properties. It uses the Keys and key-values of the properties file. It has the load and save the properties of the properties files.

pro.load(InputStream in):
This is the method that can be used to read the keys and it's values of the properties files through the help of InputStream in the properties object. It takes the object of the InputStream.

pro.getProperty(String key_name):
This is the method that can be used to search the key-values of the properties file according to it's keys. This method takes the key and search the it's values in the properties list.

Here is the code of program:

import java.io.*;
import java.util.*;

public class ReadProperty{
  String str, key;
  public static void main(String[] args) {
  ReadProperty r = new ReadProperty();
  }
  public ReadProperty(){
  try{
  int check = 0;
  while(check == 0){
  check = 1;
  BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
  System.out.print("Enter file name which has properties extension :");
  str = bf.readLine();
  File f = new File(str + ".properties");
  if(f.exists()){
  Properties pro = new Properties();
  FileInputStream in = new FileInputStream(f);
  pro.load(in);
  System.out.println("All key are given: " + pro.keySet());
  System.out.print("Enter Key : ");
  key = bf.readLine();
  String p = pro.getProperty(key);
  System.out.println(key + " : " + p);
  }
  else{
  check = 0;
  System.out.println("File not found!");
  }
  }
  }
  catch(IOException e){
  System.out.println(e.getMessage());
  }
  }
}

Download this exameple.

Download the properties file.

Advertisement

If you enjoyed this post then why not add us on Google+? Add us to your Circles



Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: April 16, 2007

Related Tutorials

Discuss: Read the Key-Value of Property File in Java   View All Comments

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:0
DMCA.com