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) {
}
# 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
Helped me a lot! It made my code really simpler!
Thanx
Great, thank U.
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
The problem with this code is that the streams are not closed after the properties are read or written
not enough
thanks !!!
clear and useful
simple, yet very useful article.
Спасибо за уроки брат.
sucks
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();
}
i forgot to put fos.close(); it should be
prop.store((fos = new FileOutputStream("yourPropertiesFile.properties")), "Author: jasonX");
fos.close();
regards,
jasonX
thx for the example. helpful
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());
}
}
}
GREAT EXAMPLE
close() statements should go inside a finally block !
properties.store(new FileOutputStream("C:/ibm_scopedb_en.pl"), "1.comment"+"\n"+"2.comment");
how to write the commnet in new line ?
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 ) { /* .... */ }
}
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 ) { /* .... */ }
}