Java Programing laungage

java.util Projects

java.util Project 1

Read the Key-Value of Property

In this page of the tutorials, we shall be learn how to read the properties key-value files in Java. This properties file provides the general text editor, which have the pair of the keys and it's values. some constructor and method is used in this program those are described bellow.

Previous Home Next

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

pro.load(InputStream in): This method is 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 method is used to search the key-values of the properties file respect of keys. This method takes the key and search the it's values in the properties list.

Example


package r4r;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;
public class readkeyValue {
String str, key;
public static void main(String[] args) {
	readkeyValue r = new readkeyValue();
}[an error occurred while processing this directive]
public readkeyValue(){
try{
int check = 0;
while(check == 0){
check = 1;
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter file name :");
str = br.readLine();



File f = new File(str + ".properties");
if(f.exists()){
Properties prop = new Properties();
FileInputStream fin = new FileInputStream(f);
prop.load(fin);
System.out.println("All key are given: " + prop.keySet());
System.out.print("Enter the Key : ");
key = br.readLine();
String p = prop.getProperty(key);
System.out.println(key + " : " + p);
}
else{
check = 0;
System.out.println("File not found!");
}
}
}
catch(IOException e){
System.out.println(e.getMessage());
}
}
}

Previous Home Next