zParacha.com

Effective programming and blogging tips by Zaheer Paracha

Subscribe:

-->
Posted by Zaheer 7 COMMENTS

Reading properties file in Java is much easier than you might have thought. Following example illustrates one simple way of reading properties from a properties file.

Let’s say we need to read from myConfig.properties file.




The properties file has following entries.

Directory = C:/prodFiles/
NumberOfFiles = 25
Extension = java

Here is the java code to read the values of these keys.

import java.util.Properties;
import java.io.*;
public class ReadValues{
   private static final String PROP_FILE="myConfig.properties";
   public void readPropertiesFile(){
       try{
  	     InputStream is = ReadValues.class.getResourceAsStream(PROP_FILE);
	     Properties prop = new Properties();
             prop.load(is);
	     String directory = prop.getProperty("Directory");
             String numberOfFiles = prop.getProperty("NumberOfFiles");
	     String  fileExtension = prop.getProperty("Extension");
             is.close();
	  /* code to use values read from the file*/
       }catch(Exception e){
         System.out.println("Failed to read from " + PROP_FILE + " file.");
       }
   }
  }

The code is quite simple and self explanatory. Let me know if you have any questions.

Bookmark and Share

Popularity: 27% [?]

If you enjoyed this post, make sure you subscribe to my RSS feed!

Related posts:

categories: Java

7 Responses

  1. Bret says:

    Well done. I appreciate the fact that you didn’t assume everyone knew how to read in a properties file with Java. It’s a simple task but worth the post. Thanks.

    Bret’s last blog post..Windows XP to Remain Available Until 2010

  2. John Doe says:

    Thanks!

  3. vivek kumar says:

    hi coud u plz tell me where u put your .properties file

  4. Zaheer says:

    Vivek, you can put your properties file in any directory as long as that directory is in your classpath. For e.g; if you save your properties file under C:\resources directory make sure that C:\resources is in your classpath. Or you can put your properties file in the same directory where you have your class file.

  5. Prabhat Atri says:

    For web applications we can put the .properties file inside your src folder so that after build all the properties file gets reflected/copied to /WEB-INF/classes folder.

    To access you can use following code snippet:

    Properties props = new Properties();
    try
    {
    ClassLoader cl = UploadDealDocs.class.getClassLoader();
    InputStream fin = cl.getResourceAsStream.(“./config.properties”);
    props.load(fin);
    uploadedFilePath = props.getProperty(“localDrivePath”);
    fin.close();
    }
    catch (Exception e1)
    {
    LOG.info(“FILE CANNOT BE READ”);
    e1.printStackTrace();
    }

    Cheers :)

  6. Rashmi says:

    Is there a different format for reading URL values from the properties file?

    My property file has

    targetUrl=https://foobar:9080/foo

    I am getting a malformed exception error. Do you think I am missing something?

    Also, how do we read integer values from the properties file?

  7. Zaheer says:

    Rashmi, you don’t have to do anything special to read the URL from properties file. Follow the example I provided and you should be able to read the URL from the file. If you can share your code I’ll be glad to help you with malformed exception.
    To read an integer value you read the value from the file as String and then convert that value to an int using valueOf method of the Integer class. In my example I read numberOfFiles from the file, here is how you can convert that to an int.
    String numberOfFiles = prop.getProperty(“NumberOfFiles”);
    int number = Integer.valueOf(numberOfFiles);

    Hope this helps, otherwise feel free to contact me directly.

Leave a Reply

Search

Sponsors

Get Adobe Flash playerPlugin by wpburn.com wordpress themes