Java Properties file examples
Normally, Java properties file is used to store project configuration data or settings. In this tutorial, we will show you how to read and write to/from a properties file.
1. Write to properties file
Set the property value, and write it into a properties file named “config.properties“.
package com.mkyong.common; import java.io.FileOutputStream; import java.io.IOException; import java.util.Properties; public class App { public static void main( String[] args ) { Properties prop = new Properties(); try { //set the properties value prop.setProperty("database", "localhost"); prop.setProperty("dbuser", "mkyong"); prop.setProperty("dbpassword", "password"); //save properties to project root folder prop.store(new FileOutputStream("config.properties"), null); } catch (IOException ex) { ex.printStackTrace(); } } }
Output
config.properties
#Mon Jan 11 18:54:40 MYT 2010 dbpassword=password database=localhost dbuser=mkyong
P.S If file path is not specified, then this properties file will be stored in your project root folder.
2. Load a properties file
Load a properties file from the file system and retrieved the property value.
package com.mkyong.common; import java.io.FileInputStream; import java.io.IOException; import java.util.Properties; public class App { public static void main( String[] args ) { Properties prop = new Properties(); try { //load a properties file prop.load(new FileInputStream("config.properties")); //get the property value and print it out System.out.println(prop.getProperty("database")); System.out.println(prop.getProperty("dbuser")); System.out.println(prop.getProperty("dbpassword")); } catch (IOException ex) { ex.printStackTrace(); } } }
Output
localhost mkyong password
3. Load a properties file from classpath
Load a properties file named “config.properties” from project classpath, and retrieved the property value.
package com.mkyong.common; import java.io.FileInputStream; import java.io.IOException; import java.utilutil.Properties; public class App { public static void main( String[] args ) { Properties prop = new Properties(); try { //load a properties file from class path, inside static method prop.load(App.class.getClassLoader().getResourceAsStream("config.properties");)); //get the property value and print it out System.out.println(prop.getProperty("database")); System.out.println(prop.getProperty("dbuser")); System.out.println(prop.getProperty("dbpassword")); } catch (IOException ex) { ex.printStackTrace(); } } }
For non-static method, use this :
prop.load(getClass().getClassLoader().getResourceAsStream("config.properties");));
References
Tags : java properties

I am typically to blogging and i actually respect your content. The article has actually peaks my interest. I’m going to bookmark your web site and hold checking for brand new information.
I completely agree.
EVERY SINGLE article I have read so far has been extremely helpful and straight to the point.
Thanks. Keep it up
Thank you! It’s useful!
Thank you! Your site is really helpfull!)))
Thanks,
Really Helped me.
thanks
Thank you
Hi All,
Can anyone please help me in finding out what all system properties are available during recovery time ?
The System properties for eg: ro.product.xxx that are available,when the device is fully booted up , is different from when the device is in recovery mode.
The requirement is to access few system properties during recovery mode.
Thanks in Advance !
Please define the ‘device’ and ‘recovery time’
thx
Device = Android Tablet
Recovery Time = I didn’t get this. This recovery mode is same as when
we see the recovery screen for OTA updates.
Hi MK,thanks for your post,but my requirement is little different.I want edit an existing property file at runtime.Suppose there is key value like name=sanjib but at runtime i want to change it to name=sanjibdhar.Is it possible and how??
i want to read the metadata of TIFF image.Can u plz tel me dat how would it possible by using JAVA ADVANCED IMAGING(JAI).
Just wanted to say that THIS IS AWESOME. I’ve been looking for straight-up source code on how to do this for a while. Perfect. THANKS!
five. Keep nasal passages clear
Hello,
I am trying to make the property file, but while i am adding any special character in the value of the key value pair, it appends a ‘/’ in front of that.
like
prop.setProperty(“database.url”, “jdbc:oracle:thin:@localhost:1521:xe”);
but in the property file, it is stored as
database.url=jdbc\:oracle\:thin\:@localhost\:1521\:xe
Please help, its quite urgent.
Thanks in advance
The \ character is added to escape the specials chars like :. If you test your code with a simple
you will see that your property is as expected, without \ char.
Thnx for the reply, but I want to write that in the property file as it is(*its kind of requirement), and the ordering is also not right. Its getting saved randomly.
I want it to be in the manner, I am writing it.
So, any help on this..???
I think properties are optimized internally with storing algorithm, that’s why the value is
put randomly in the file. If you want to write to a file with the certain structure just write to it as on a normal file. Like here http://www.exampledepot.com/egs/java.io/WriteToFile.html. Also read this about what you can and can’t do with a properties file:
http://docs.oracle.com/javase/tutorial/essential/environment/properties.html
Gracias !!!!
thanks for the post. I am trying to read all my xpaths for selenium. This post helped me
When I write my Properties to a file, there is always a ‘\’ at the 40th character of each key-value pair. But the properties file work perfectly after reading back. Why?
How to store property in the following format:
property1_name = list_of_values1
property2_name = list_of_values2
I use Properties.store() method to store my properties. But it was add a ‘\’ at the 40th characters of each line, i.e. My…..Properties.lastDate2=30/07/2012 13\:35
I read back the file with Properties.load() and it works perfectly. Is there a way to get rid of the ‘\’?
thank you for the examples
Thanks you so much …you posts helped me a llot..plz keep up the good work :)
Thank you so much…you saved me from lots of unnecessary hard work..had searched for solution at so many places but your posting helped me greatly …thanks
Hi,
How to remove the timestamp added to the file, on writing?
could you explane what this part of code in .properties file will do?
logPropFileName = D:\\Program Files\\Corillian\\DMS-PA\\conf\\PA-log4j_cnet.properties
paHistoryFile=D:\\FTP_Dir\\Current\\PaymentHistoryCNet.xml
paReturnFile=D:\\FTP_Dir\\Current\\ReturnCNet.xml
paRejectFile=D:\\FTP_Dir\\Current\\RejectCNet.xml
paCheckFile=D:\\FTP_Dir\\Current\\CheckCNet.xml
I am goning through the some BAT files and i am not getting this part
com.corin.pa.facade.CheckUpdateFacade d:/”program files”/Corin/DM-PA/scripts/system_b.properties
what does this part is doing actually?
Hi,
to my understanding of file operation, when loading properities from a file, the FileInputStream shall be closed.
There should be added 2 more things, fin should be initialized and the close operation should be on try catch:
pre lang=”java”>
FileInputStream fin = null;
try {
// load a properties file
fin = new FileInputStream(“config.properties”);
prop.load(fin);
// get the property value and print it out
System.out.println(prop.getProperty(“database”));
System.out.println(prop.getProperty(“dbuser”));
System.out.println(prop.getProperty(“dbpassword”));
} catch (IOException ex) {
ex.printStackTrace();
} finally {
try {
fin.close();
} catch (IOException e) {
}
}
thank you
how should i update the .property file by a user interface html form..?
This worked right out of the box. Perfect.
hello
I need to get only the three first lines of a .properties file in Java.
The problem I am facing is that I’m getting all the lines of the file with that method:
I need to fill in an array with the three first properties I get, so how can I read only the three first lines?
Hi All,
Please refer the below mentioned blog.. Solves the problem of FileIOnotfoundException
It;s simple technique to create a new package and include the property file within it..
So that makes the property file available within the JAR file.
http://viralpatel.net/blogs/2009/10/loading-java-properties-files.html
Thanks,
Ranjan
That did it. Thank you.
Peter
i am sorry my prev message i could nt see properly that is why i’m giving again
i added the class path as whole path, and also from project
value=”classpath:/home/dev06/filesys/config/info.properties”
and
value = “classpath: filesys/config/info.properties”
i am getting FileIOnotfoundException and it cound nt read or load
I have the same problem in a netbeans project. I’m trying to read the config file from another folder (actually a package) in the project, so I’ve got:
main project folder (package)
|
+- main class
|
+- sub-folder (package)
| |
| +- java file from which the call to load the properties file is made
|
+- sub-folder (package) for the properties
|
+- properties file
I’ve tried every type of relative path I can think of:
* config.properties
* properties/config.properties
* ../properties/config.properties
* ../../properties/config.properties
… and some more, but nothing seems to work. I still get a file not found exception.
Any suggestions gratefully received.
Cheers
Peter
Below code works if your .properties file is present in src/main/resources folder
and this code does the trick to keep ur .properties file out side src and u can customize at any point of time
in this way
Thanks. I guess that works if you’re using a framework, but I’m not. It’s just a plain vanilla (NetBeans) Java project.
(I couldn’t find a framework for Java that wasn’t either Web or Java EE centric).
Cheers
Peter
Hello, how to read .properties file, present in outside the src folder
for example i have a .properties file. i dont want to keep this in src folder, so i created a new folder config, and i added my .properties file
i gave code as class=”org.springframework.beans.factory.config.PropertyPlaceholderConfigurer”>
and also class=”org.springframework.beans.factory.config.PropertyPlaceholderConfigurer”>
but still i am gettingthis message
Could not load properties; nested exception is java.io.FileNotFoundException: class path resource [resource/Information.properties] cannot be opened because it does not exist
any help will be appreciated
This is Nice.
Simple and Straight :)
very helpful.Thanx
You should close the stream used to write the properties to once you’re done writing.
http://helpdesk.objects.com.au/java/how-to-store-values-in-a-properties-file
Quick question, what if I would like to write to the file multiple times. I tried this solution, but it creates a new file and I loose previous written sections.
Thanks,
Homer
can i have your code ?
thanx for your examples
Always find your hints/notes (no matter how small) very helpful!!!
Any advice on loading properties several directories up? Unfortunately relative and “../” format for going back a directory doesnt seem to work.
really so good .i think this type of sample will be very useful to all
Hi i nee small information reg to properties file in struts1.3
i want to add some extra msg to properties file is it possible?????if s kindly reply me ASAP
i want to print action message like “20 records inserted successfully” here 20 is dynamic i don’t know the exaact value……
Swathi.. go throw this link…
http://www.coderanch.com/t/531584/Struts/change-key-value-pair-properties