Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Sign In with Facebook Sign In with Google Sign In with OpenID

Categories

We have migrated to a new platform! Please note that you will need to reset your password to log in (your credentials are still in-tact though). Please contact lee@programmersheaven.com if you have questions.
Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

Properties

vkumarkhvkumarkh Posts: 2Member
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

Comments

  • bad_coderbad_coder Posts: 17Member
    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();
    }
    }
Sign In or Register to comment.