We help IT Professionals succeed at work.

property file

klnhk
klnhk asked
on
Medium Priority
267 Views
Last Modified: 2013-11-23
I want to know how to use property file (Hello.property)  in java program, can any one show me a simple example?
Comment
Watch Question

Commented:
Hi klnhk,


I happen to have this test sample about properties in Java:
___________________________________________________________# Hello.properties: START
Greeting=Hello World!
# Hello.properties: ENDS
___________________________________________________________
/**
 * doron.util.GenUtils: START
 */
package doron.util;

/**
 * @author       Doron Barak
 * @version      1.0
 */

import java.io.*;
import java.util.*;

public class GenUtils {
     public static Properties getProperties(String fileName) {
          Properties properties = null;
          try {
               InputStream inputStream = new FileInputStream(getPropertyFile(fileName));
               properties = new Properties();
               properties.load(inputStream);
               inputStream.close();
          } catch (Exception e) {
          }
          return properties;
     }

     public static File getPropertyFile(String fileName) {
          File propertyFile = null;
          String paths = System.getProperty("java.class.path");
          StringTokenizer st = new StringTokenizer(paths, File.pathSeparator);
          while (st.hasMoreElements()) {
               String ss = (String)st.nextElement();
               try {
                    File dir = new File(ss);
                    while (!dir.isDirectory()) {
                         dir = new File(dir.getParent());
                    }
                    propertyFile = new File(dir, fileName);
                    if (propertyFile.exists()) {
                         break;
                    } else {
                         propertyFile = null;
                    }
               } catch (Exception ex) {
                    propertyFile = null;
               }
          }
          return propertyFile;
     }
}
/**
 * doron.util.GenUtils: ENDS
 */
___________________________________________________________
/**
 * doron.tests.Tester: START
 */
package doron.tests;

/**
 * @author       Doron Barak
 * @version      1.0
 */

import doron.util.*;
import java.util.*;

public class Tester {
     private Properties prp;

     public Tester() {
          prp = GenUtils.getProperties("Hello.properties");
     }

     public void doTest() {
          System.out.println(prp.getProperty("Greeting"));
     }

     public void main(String[] args) {
          Tester tst = new Tester();
          tst.doTest();
     }
}
/**
 * doron.tests.Tester: ENDS
 */
___________________________________________________________
First of all, I recommend reading a bit about the java.util.Properties class and about how property files are built. The "Hello.properties" file contains one value under the key "Greeting". The GenUtils class can locate a property file in the current CLASSPATH (as long as you've saved you property file IN one of the paths on the CLASSPATH) and load it into a Properties object instance. Once you've got the Properties object instance, you can read a single property value with the "getProperty(key)" method. I suggest you look into the JavaDoc for the Properties class for more information.


Good luck,
Doron
Your history reflects that you have asked a total of 38 questions at this site and only finalized 10 of them.  I think you'll agree this is not fair to the experts who have stepped in to help you, and it is against our Guidelines and Member Agreement, listed on the left under Help Desk.

I will update all your open items with a request to finalize them so that you are advised by Email of their open status and can quickly navigate through them and complete them.

If you need help to split points, process a refund and move to PAQ at zero, or otherwise special handle this question, please let us know.  I will monitor them all, and as usual, appreciate any expert input here.

Please also refer to these links:
http://www.experts-exchange.com/jsp/cmtyHelpDesk.jsp
http://www.experts-exchange.com/jsp/cmtyQuestAnswer.jsp
http://www.experts-exchange.com/jsp/infoMemberAgreement.jsp

Thanks,

Moondancer
Community Support Moderator @ Experts Exchange

Explore More ContentExplore courses, solutions, and other research materials related to this topic.