How to read properties file in Java.
Apr 08
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.
If you enjoyed this post, make sure you subscribe to my RSS feed!






Apr 09, 2008 @ 03:34:04
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
May 12, 2008 @ 09:13:04
Thanks!
Jun 11, 2008 @ 01:12:51
hi coud u plz tell me where u put your .properties file
Jun 11, 2008 @ 20:43:58
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.
Jul 23, 2008 @ 04:01:07
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
Jul 08, 2009 @ 14:34:34
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?
Jul 09, 2009 @ 09:27:48
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.
Dec 06, 2010 @ 03:23:54
Hi Zaheer,
it’s excellent one. i was trying with file input stream as shown below, but application unable to catch the file. getting file not found exception, after replacing these with your above eg. now it is working fine. thank you very much.
and may i know the fault in my below code, y i am getting errror ??
File propsFile = new File(filePath);
FileInputStream fileInputStream = new FileInputStream(propsFile);
prop.load(new FileInputStream(“db.properties”));
System.out.println(“Driver Name : “+prop.getProperty(“driverName”));
Dec 23, 2010 @ 23:56:19
Hai Friends,
i need more clarification regarding the properties file and how to retrive the values from the properties to a java file.
and provide more examples
Dec 23, 2010 @ 23:57:21
Hai
provide more examples
Oct 15, 2011 @ 00:42:21
Cna we read from properties file without using InputStream. I mean I want asingle line access to key in properties file through my swings program.
Nov 10, 2011 @ 07:55:12
@Rashim
I think you have to escape “:” and “=”. So the new line in your properties file would be:
targetUrl=https\://foobar:9080/foo
Hope this helps.
Nov 25, 2011 @ 05:16:05
thanks friend
Nov 30, 2011 @ 12:47:42
Nice, but how do you read integer values? Or doubles, floats, etc… Can it only read string values?
Feb 17, 2012 @ 05:30:18
Retrieve data from property file specially for database connection
Get Database connection parameters from property file
Jan 07, 2013 @ 01:58:02
This article is very useful…Thanks…