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

Ads

Tutorials   
How to read properties file in Java? Generate random numbers in Java Cloneable Interface in Java What is the flaw with the Stack class? List In Java Java Stack Example HashSet In Java Enumeration in java How to generate random number in java Iterator in java Queue in java Transient Java Keyword clone method in Java Java Queue example Logger in Java Java current date - How to get current date in Java Collection : LinkedList Example Collection : ArrayList Example Random alpha numeric string of fixed size Java List Iterator Example The Hashtable Class The BitSet class Java Vector Java Enumeration Data Structures in Java Java Transient Variable Java Date Format Example Java Properties File Example Map.Entry Java Interface Java Property File java preferences,Change the value of Preferences by user Retrieving a Preference Node Java hasNext Write Keys and Values to the Properties file in Java Get All Keys and Values of the Properties files in Java Properties file in Java Properties file in Java Determining If a Preference Node Contains a Specific Value Change the value of Preferences by user Clone method example in Java Implementing a Simple Event Notifier Determining When a Preference Node Is Added or Removed Listening for Changes to Preference Values in a Preference Node Importing Preferences Finding a Preference in a Preference Tree Retrieving the Parent and Child Nodes of a Preference Node Determining If a Preference Node Exists Removing a Preference Node Creating a Preference Node Getting the Maximum Size of a Preference Key and Value
Ads

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 video tutorial of: "How to Read the Key-Value of Property File in Java?"

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.

Advertisements

Ads
Share on Google+Share on Google+

Read the Key-Value of Property File in Java

Posted on: April 16, 2007 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Advertisements

 

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

Ads

 

Ads