// 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
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 ) { /* .... */ }
}
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
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();
}
Lots of good java code is available here
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
I think you should use C:\\user.properties.txt i.e BACKSLASH instead of FORWARD SLASH
I think you should use C:\\user.properties.txt i.e BACKSLASH instead of FORWARD SLASH
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
I've found the solution for my problem.
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,
Thanks, the example helped me a lot.
where should be these property files kept????
this looks difficult, think ill give up! www.callaways.co.uk
its very helpfull
[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.
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.
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
Does this code really work?
Great post. I'm subscribing now for future reads.
Can't wait until your next post. Great info.
when i am using property file in web application it is throwing null pointer exception
but that is working in normal java
Awesome articles. I bookmarked and will come back.
You make some good points. I like your point of view.M
Awesome articles. I bookmarked and will come back.
You make some good points. I like your point of view.
You make some good points. I like your point of view.
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.
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.;)
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.
Coach Online
Thanks bro.. :)
Shortly UGG Australia Boots ended up viewed all above the U. Even people who don't personal a pair identify the models and make
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
Gud one. Thanks.
ccpnhi
Hello! eeefbgf interesting eeefbgf site! I'm really like it! Very, very eeefbgf good!
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
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!
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.
How to Remove all ths data from a properties file????some1 plz help
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.