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.
Popularity: 27% [?]
If you enjoyed this post, make sure you subscribe to my RSS feed!Related posts:
- How to read properties file in Spring.
- How to write to properties file in Java
- How to search for a string in a file using Java regular expression.
- Dynamically loading an external JavaScript or CSS file
- Import static elements for more readable Java code






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
Thanks!
hi coud u plz tell me where u put your .properties file
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.
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
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?
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.