Java

Moderators: zibadian
Number of threads: 7832
Number of posts: 18231

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
Properties Posted by vkumarkh on 10 May 2011 at 9:56 AM
Hi All,
This is to ask about java.util.Properties;
As we use the same to deal with Properties files, I would like to know that, if I need to modify the value of some key, or delete the existing key value pairs, what measure do we need to take.
I tried and found that Properties.setProperty is helpful in setting the property in property file but Properties.remove removes temprorily and does not reflect the changes in actual properties file, i.e. file remains the same and key value pair does not removed from the file.
Please help and it would be great if anyone would provide the code snippet for the same.
R's
Vinay
Report
Re: Properties Posted by bad_coder on 8 Sept 2011 at 10:27 AM
After you call remove you need to store the data again. This will cause the value in the file to reflect the property object. Here is an example:

package mess;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;

public class P2
{
Properties p = new Properties ();

public void exe ()
{
try
{
p.load(new FileInputStream("P2.properties"));
} catch (IOException e)
{
System.out.println("The property file will be created");
}
System.out.println(">"+p.toString());

p.setProperty("Hello", "ME");
p.setProperty("ClassName", this.getClass().getSimpleName());
System.out.println(">"+p.toString());


p.remove("Hello");
System.out.println(">"+p.toString());
try
{
p.store(new FileOutputStream("P2.properties"), null);
} catch (IOException e)
{
e.printStackTrace();
}
}

public static void main (String []args)
{
new P2().exe();
}
}



 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.