Core Java| JSP| Servlets| XML| EJB| JEE5| Web Services| J2ME| Glossary| Questions?

 

 

 

 

 

 

 

 

 

 

 

 

 

Search Tutorials

Latest Questions
Comments
 
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

 

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.

                         

» View all related tutorials
Related Tags: c string ide class reference io references size sed get vi key value field fields this id length max preferences

Leave your comment:

Name:

Email:

URL:

Title:

Comments:


Enter Code:

Audio Version
Reload Image
 

Note: Emails will not be visible or used in any way, and are not required. Please keep comments relevant. Any content deemed inappropriate or offensive may be edited and/or deleted.

No HTML code is allowed. Line breaks will be converted automatically. URLs will be auto-linked. Please use BBCode to format your text.

Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 

Current Comments

4 comments so far (
post your own) View All Comments Latest 10 Comments:

what if I want to put the .properties file in some location in C:/
Please explain how to get the file in that case

Posted by ravi on Tuesday, 04.29.08 @ 14:41pm | #58112

The program is working, but my question is. if i am giving the .property file and .java file, its working but i want to give the .property file in different directory. How to do it. can u plz tell me !!

Posted by swamy on Friday, 01.25.08 @ 18:36pm | #46142

i write above given program but i am not getting where i should put it i tried classpath setting
but it's not working it is not getting file path

Posted by bhupesh on Tuesday, 10.16.07 @ 12:00pm | #33889

Thank you to write this tutorial, it saved time when i was looking for how to read key/value from a .properties file.
Thanka again!

Posted by Clement on Friday, 06.15.07 @ 08:54am | #19280

Training Courses
Tell A Friend
Your Friend Name
Software Solutions
Least Viewed
Most Rated
Recently Viewed
Search Tutorials

 

 
 

Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Search Engine | News Archive | Jboss 3.0 tutorial | Free Linux CD's | Forum | Blogs

About Us | Advertising On RoseIndia.net  | Site Map

India News

Indian Software Development Company | iPhone Development Company in India | Flex Development Company in India | Java Training Delhi | Java Training at Noida |

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright © 2008. All rights reserved.