Home | Ajax | BioInformatics | Dojo | EAI | EJB | Hibernate | J2ME | Java | Java Glossary | Java Servlets | JavaScript | Jboss | JDBC | JDO | Jmeter | JSF | JSP | JUnit | Maven | MySQL | Spring Framework | SQL | Struts | Technology | WAP | Web Services | XML
 
 
Search All Tutorials
  

 
Programming Tutorials: Ajax | Articles | JSP | Bioinformatics | Database | Free Books | Hibernate | J2EE | J2ME | Java | JavaScript | JDBC | JMS | Linux | MS Technology | PHP | RMI | Web-Services | Servlets | Struts | UML
 
Java
  JDO Tutorials
  EAI Articles
  Struts Tutorials
  Java Tutorials
  Java Certification
  Java Applet
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. 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.

                         

Facing Programming Problem?
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

Leave your comment:

Name:

Email:

URL:

Title:

Comments:


Enter Code:

 

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.

Hot Web Programming Job

Java String toLowerCase Example
Java String toCharArray Example
Java String substring Example
Java String indexOf Example
Java String startsWith Example
Java String hashCode Example
Java String matches Example
Java String length Example
Java String lastIndexOf Example
Java String isEmpty Example
Java String equalsIgnoreCase Example
Java String equals Example
Java String endsWith Example
Java String copyValueOf Example
Java String contentEquals Example
  EAI Articles
  Java Certification
Tell A Friend
Your Friend Name
Search Tutorials

 

 
 
Browse all Java Tutorials
Java JSP Struts Servlets Hibernate XML
Ajax JDBC EJB MySQL JavaScript JSF
Maven2 Tutorial JEE5 Tutorial Java Threading Tutorial Photoshop Tutorials Linux Technology
Technology Revolutions Eclipse Spring Tutorial Bioinformatics Tutorials Tools SQL
 

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

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

Copyright © 2007. All rights reserved.