Java Properties file examples
Properties file is always used to store the configuration data or settings. In this example you will learn how to read or write properties file and read and set property value in properties file.
Properties file example 1
In this example, set the property value and write it into a properties file named “config.properties”. The new saved properties file will stored at your project root folder.
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
The content of the file “config.properties”
#Mon Jan 11 18:54:40 MYT 2010 dbpassword=password database=localhost dbuser=mkyong
Properties file example 2
In this example, load a properties file “config.properties” and retrieved the saved 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
Reference
http://java.sun.com/j2se/1.4.2/docs/api/java/util/Properties.html
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
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
[...] Mkyong.com – Java Properties file examples TwitterLinkedInFacebookMoreDiggEmailRedditPrintStumbleUponLike this:LikeBe the first to like this [...]
[...] Mkyong.com – Java Properties file examples TwitterLinkedInFacebookMoreDiggEmailRedditPrintStumbleUponLike this:LikeBe the first to like this [...]
[...] Java Properties file examples MSN: Chewbaka@RSBuddy.com Bots I Support: ChewbakaChinner, ChewbakaCooker [...]
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