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 ) { /* .... */ }
}

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.