Reading and Writing a Properties File

// Read properties file.
Properties properties = new Properties();
try {
    properties.load(new FileInputStream("filename.properties"));
} catch (IOException e) {
}

// Write properties file.
try {
    properties.store(new FileOutputStream("filename.properties"), null);
} catch (IOException e) {
}
Here is an example of the contents of a properties file:
# a comment
! a comment

a = a string
b = a string with escape sequences \t \n \r \\ \" \' \ (space) \u0123
c = a string with a continuation line \
    continuation line
d.e.f = another string

Comments

24 Feb 2010 - 1:19pm by Anonymous (not verified)

Helped me a lot! It made my code really simpler!
Thanx

3 Mar 2010 - 2:46am by Rita (not verified)

Great, thank U.

8 Mar 2010 - 11:52pm by Hari Kishan (not verified)

I am facing problem with "-" in th properties file.Can you please help me escaping the "-" in the properties file.My mail id is kishan.annam@gmail.com

16 Mar 2010 - 3:24am by Maurice (not verified)

The problem with this code is that the streams are not closed after the properties are read or written

17 Mar 2010 - 8:29pm by Anonymous (not verified)

not enough

21 Mar 2010 - 2:59am by Anonymous (not verified)

thanks !!!
clear and useful

25 Mar 2010 - 1:18pm by thilinamb (not verified)

simple, yet very useful article.

27 Mar 2010 - 2:27am by Леонид (not verified)

Спасибо за уроки брат.

5 Apr 2010 - 1:09am by cracatoa (not verified)

sucks

5 Apr 2010 - 1:22am by cracatoa (not verified)

Properties prop = new Properties();

try {
FileInputStream fis = new FileInputStream("yourPropertiesFile.properties");
prop.load(fis);
// You can do something here like getting the value of a key. Example
String str = prop.getProperty("yourKey");
System.out.println(str);

fis.close();

FileOutputStream fos = null;
// Setting a key=value pair
prop.setProperty("yourKey", "theValue");
prop.store((fos = new FileOutputStream("yourPropertiesFile.properties")), "Author: jasonX");
} catch(IOException e) {
e.printStackTrace();
}

5 Apr 2010 - 1:25am by cracatoa (not verified)

i forgot to put fos.close(); it should be

prop.store((fos = new FileOutputStream("yourPropertiesFile.properties")), "Author: jasonX");
fos.close();

regards,
jasonX

30 Apr 2010 - 6:49am by onebauknecht (not verified)

thx for the example. helpful

4 May 2010 - 10:25am by LATESH SARODE (not verified)

import java.io.*;
import java.util.*;

public class WriteProperty{
String str, key, val;
public static void main(String[] args) {
WriteProperty w = new WriteProperty();
}
public WriteProperty(){
try{
int check=0;
while(check == 0){
check=1;
BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter file name which has properties extension:");
str = bf.readLine();
Properties pro = new Properties();
File f = new File(str + ".properties");
if(!f.exists()){
check=0;
System.out.println("File not found!");
}
else{
FileInputStream in = new FileInputStream(f);
pro.load(in);
System.out.print("Enter Key : ");
key = bf.readLine();
System.out.print("Enter Value : ");
val = bf.readLine();
pro.setProperty(key, val);
pro.store(new FileOutputStream(str + ".properties"),null);
System.out.println("Operation completly successfuly!");
}
}
}
catch(IOException e){
System.out.println(e.getMessage());
}
}
}

10 Jun 2010 - 9:17am by Anonymous (not verified)

GREAT EXAMPLE

22 Jun 2010 - 8:44pm by Anonymous (not verified)

close() statements should go inside a finally block !

8 Jul 2010 - 12:28am by Sans (not verified)

properties.store(new FileOutputStream("C:/ibm_scopedb_en.pl"), "1.comment"+"\n"+"2.comment");
how to write the commnet in new line ?

31 Aug 2010 - 2:24am by Yarick (not verified)

The Example is VERY bad, as it provoks newbies not to close the opened streams.

Shame on you!!!

P.S. not so bad example:
Properties properties = new Properties();
InputStream is = null;
try {
is = new FileInputStream( "filename.properties" );
properties.load( new FileInputStream("filename.properties") );
} catch( IOException e ) {
// ...
} finally {
if( null != is ) try { is.close(); } catch( IOException e ) { /* .... */ }
}

31 Aug 2010 - 2:26am by Yarick (not verified)

CORRECTED:
Properties properties = new Properties();
InputStream is = null;
try {
is = new FileInputStream( "filename.properties" );
properties.load( is );
} catch( IOException e ) {
// ...
} finally {
if( null != is ) try { is.close(); } catch( IOException e ) { /* .... */ }
}

6 Sep 2010 - 1:16am by Anonymous (not verified)

Anyone knows how to include several properties files on a properties file ?
include = file01.properties
include = file02.properties
or
include = file01.properties,file02.properties
or
include = file01,file02

11 Sep 2010 - 7:45pm by alvaro (not verified)

i tried this. but nothig happended. my file.properties wasn't modificated.

Properties prop = new Properties();
try {

FileOutputStream fileOutputStream = null;
prop.setProperty("-----", "----");
prop.store((fileOutputStream = new FileOutputStream(getClass().getResource("/resources/prueba.properties").getPath())),null);
fileOutputStream.close();
} catch (IOException e) {
e.printStackTrace();
}

27 Sep 2010 - 11:17pm by Chandreshsing (not verified)

Lots of good java code is available here

22 Oct 2010 - 3:42pm by Anonymous (not verified)

I am getting this error on prop.load(in). Can you help.

Properties prop = new Properties();

FileInputStream in = new FileInputStream("C://user.properties.txt"); prop.load(in);

Multiple markers at this line
- Syntax error on token(s), misplaced construct(s)
- Syntax error on token "in", VariableDeclaratorId expected after
this token

22 Nov 2010 - 3:28am by Anonymous (not verified)

I think you should use C:\\user.properties.txt i.e BACKSLASH instead of FORWARD SLASH

22 Nov 2010 - 3:28am by Anonymous (not verified)

I think you should use C:\\user.properties.txt i.e BACKSLASH instead of FORWARD SLASH

23 Nov 2010 - 4:22am by Anonymous (not verified)

Hi everyone,
I've tried this example : it works but if you have two programs that must write to the same file, the result of the first program is lost. I thought that the result of the second program should be added to the file (I use another key in the second program).
Do you have an idea why ?
thanks and regards

23 Nov 2010 - 8:40am by Anonymous (not verified)

I've found the solution for my problem.

25 Nov 2010 - 4:23am by Anonymous (not verified)

Hi,

My Requirement is to read all the key-value pairs from a property file and after that I would update one of the value (pertaining to a key) and I need to store the same. When I use the store() method, the order of all the key-value pairs in my original file is getting changed. Is there any other way of doing it..?

Thanks,

28 Nov 2010 - 4:10am by Rabeea (not verified)

Thanks, the example helped me a lot.

22 Dec 2010 - 11:06pm by Anonymous (not verified)

where should be these property files kept????

24 Dec 2010 - 3:37am by brighton estate agents (not verified)

this looks difficult, think ill give up! www.callaways.co.uk

3 Jan 2011 - 6:34am by kumar from indai (not verified)

its very helpfull

18 Feb 2011 - 9:46am by thhui (not verified)

[cdoe]
Properties props = new Properties();
props.load(getClass().getResourceAsStream("message.properties"));
[/code]

This will load message.properties file just on the same folder as the main java file.

19 Feb 2011 - 10:55pm by Anonymous (not verified)

Sites like this really annoy me. The example is a very poor demonstration of correct Java programming. The FileInputStream must be closed after the property itself is loaded.

24 Feb 2011 - 5:49am by Paolo (not verified)

Is there a way to insert a comment inside the value of a property?
I mean something like:
propname=value #and this is a comment

this doesn't work for me, all the line after the "=" is considered as part of the value

1 Jun 2011 - 10:23pm by mobile porting (not verified)

Does this code really work?

4 Jun 2011 - 1:58am by kitesurfing (not verified)

Great post. I'm subscribing now for future reads.

7 Jun 2011 - 5:17pm by Polish Dating (not verified)

Can't wait until your next post. Great info.

8 Jun 2011 - 4:46am by Anonymous (not verified)

when i am using property file in web application it is throwing null pointer exception
but that is working in normal java

14 Jun 2011 - 7:35pm by kitesurf (not verified)

Awesome articles. I bookmarked and will come back.

14 Jun 2011 - 7:36pm by Polish Dating (not verified)

You make some good points. I like your point of view.M

20 Jun 2011 - 8:45pm by kitesurf (not verified)

Awesome articles. I bookmarked and will come back.

20 Jun 2011 - 8:45pm by polishdate (not verified)

You make some good points. I like your point of view.

26 Jun 2011 - 6:33pm by kiteboarding (not verified)

You make some good points. I like your point of view.

27 Jun 2011 - 6:40am by Anonymous (not verified)

What about if I don't want server to restart when I change some property file value.
I am using Tomcat as server and we have GUI and I need when user change any property file value.. that should be reflected without restarting the server..

Any solution for this prob.

30 Jun 2011 - 6:58am by archie (not verified)

I am also working on something like this and have researched a lot..When I use absolute path in the FileInputStream() then it reads the file but whenever I try to give the relative path..It throws a FileNotFoundException.
I even tried using getResourceAsStream(), and in that case it shows me nullpointerexception..I am totally not sure what to do in this regard now..Does anybody have an idea about what is happening and how I could resolve it? Thanks in advance.;)

16 Jul 2011 - 7:26am by personal loans (not verified)

I received 1 st personal loans when I was not very old and that supported me a lot. Nevertheless, I need the bank loan once again.

20 Aug 2011 - 1:11am by Coach Online (not verified)

Coach Online

28 Sep 2011 - 10:16pm by Anonymous (not verified)

Thanks bro.. :)

25 Oct 2011 - 1:33am by UGG Pas Cher (not verified)

Shortly UGG Australia Boots ended up viewed all above the U. Even people who don't personal a pair identify the models and make

27 Oct 2011 - 5:18pm by Cheap air jordan (not verified)

The Cheap air jordan became famous when Michael Jordan came into use in the field. They were not the traditional white peaks that basketball players have traditionally been, and the National Basketball Association officials forbade him to use them. Michael Jordan ignored the ban and was fined $ 5,000 each time you used them during a match. The controversy became even more popular shoes and quickly became the most sought after shoes in this country and around the world.air jordan 7 air jordan 11 cheap air jordans for sale air jordan for sale discount air jordan cheap air jordan Shoes cheap air jordan sale Air jordan Retro air jordan 5 version of the sneaker is truly the best-selling line of shoe size, so many to buy these for their support while playing basketball, not just their appearance. The boys want to emulate Michael Jordan basketball moves on the ground of particular benefit to the sneakers.air jordan 5 air jordan 5 retro air jordan retro 5 jordan shoes 5 jordan 5 shoes air jordan 5 shoes air jordan 5 for sale cheap air jordan 5

31 Oct 2011 - 10:00pm by Mital Pritmani (not verified)

Gud one. Thanks.

10 Jan 2012 - 10:15pm by udcsqtqn (not verified)

ccpnhi

12 Jan 2012 - 5:26pm by Pharmg808 (not verified)

Hello! eeefbgf interesting eeefbgf site! I'm really like it! Very, very eeefbgf good!

15 Jan 2012 - 2:28am by zeeshan rehman http://biztix.ca (not verified)

HI

I have learned a lot from sites like this. In fact I don't depend on just books/classroom lectures. I would rather search for any examples for my tasks and test them right away and then modify again and again to suite my uses.

I will share my codes and applications like this manner and would love to share my experience with my community of java coders.

P.S. Please don't take bad comments seriously, if there are some codes not so good for anybody then they should find a good code and suggest in a positive way.

peace

12 Mar 2012 - 1:31am by Cheap New Era Hats (not verified)

Welcome to Cheap New Era Caps Shop!We offer wholesale cheap new era caps,New Era 59fifty Caps,New Era Snapback Caps,Wholesale New Era Caps,Wholesale New Era Fitted Caps. Fast shipping, free tax,lowest price,authentic quality!

10 May 2012 - 8:02pm by sunglasses hut (not verified)

This will make any woman cheap Oakley sunglasses, sunglasses hutlike a movie star feeling. Sir alex ferguson appears to be married, not only has charm will not reduce the favour. However, sunglassesall these Oakley sunglasses design inspiration, not imitate or fake.

11 May 2012 - 4:33am by Anonymous (not verified)

How to Remove all ths data from a properties file????some1 plz help

15 May 2012 - 6:11pm by adidas jeremy scott shoes (not verified)

This Adidas sneaker is known as a Adidas Porsche Bounce S classic tennis shoe that we Adidas Porsche Bounce SL have seen in many variations.This version of Adidas Stan Smith is also unique compared to the previews versions as it doesn't have Adidas Porsche Design Golf the usual perforated three stripes on the side panels.It also Adidas Porsche Design Bounce includes gray accents found Adidas Porsche Bounce S2 on tag on the tongue area carrying the Adidas Originals logo.

Post a comment

More information about formatting options

CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
Image CAPTCHA
Enter the characters shown in the image. Ignore spaces and be careful about upper and lower case.