1

Example.properties

user=somePerson
env=linux
file=mpg

properties.java class

propertiestTest.java
   {
     Properties props = new Properties();
     props.setProperty("user", "GodIsUser");
     final File propsFile = new File(someDir/Example.properties");

     props.store(new FileOutputStream(propsFile), "");
}

reseult of Example.properties

user=GodIsUser

and all other entries are deleted

5 Answers 5

7

You need to populate it from the file first using props.load:

final File propsFile = new File("someDir/Example.properties");
Properties props = new Properties();
props.load(new FileInputStream(propsFile));
// make changes
props.save(new FileOutputStream(propsFile), "");
1
  • 1
    Thank you! Response time was awesome! Sep 29, 2011 at 18:42
2

You should use props.load(inStream) to load the existing props first.
first use Properties.load(), and only after it: modify the properties by using Properties.setProperty()

1
  1. Read the properties file into a Properties object.
  2. Add the new properties.
  3. Store the newly updated Properties object.

Step 1 above is the key.

1

You can use put:

//Load the props
final File propsFile = new File(someDir/Example.properties");

Properties props = load(new FileOutputStream(propsFile));
props.put("user", "GodIsUser");

props.store(new FileOutputStream(propsFile), "");
0
try {
        FileInputStream fileName=new FileInputStream(fname);
        Properties props = new Properties();
        props.load(fileName);
        props.setProperty(Id, value);
        fileName.close();
        FileOutputStream outFileName=new FileOutputStream(fname);
        props.store(outFileName, "");
        outFileName.close();
        } catch (IOException io) {

            io.printStackTrace();
        } 

If there is a need close the file , do it as mentioned above.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.