How to read Key-Value of property file using Java?


       Read the Key-Value of Property File 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());
}
}
}

People who read this post also read :



0 comments:

Post a Comment

Share

Twitter Delicious Facebook Digg Stumbleupon Favorites More