0

I am trying to update properties file as below.

        Properties props = new Properties();

        FileInputStream in = new FileInputStream(filepath);
        props.load(in);
        in.close();

        FileOutputStream out = new FileOutputStream(filepath);
        props.setProperty(key, value);
        props.store(out,"fileupdated");

        out.close();

But always I notice that newly added properties are added some where in middle of the file and I want it to be added only at the end. Please advise how should I resolve this.

Also, I have requirement to update core properties file. So in case if there are any issues updating the properties file, my server will stop running. Just because even a miss of single existing property will impact. So i am planning to create a temp file and if writing is succesfull i will rename to original file. PLease let me know if there are any other better approaches.

Skanda
  • 835
  • 1
  • 15
  • 32
  • 1
    possible duplicate of [How can I write Java properties in a defined order?](http://stackoverflow.com/questions/17011108/how-can-i-write-java-properties-in-a-defined-order) – Seelenvirtuose Jun 24 '15 at 17:47
  • Thanks for giving suggestion. But, can anyone please answer my second part of the question. – Skanda Jun 24 '15 at 17:57
  • after adding values to properties check if it has all the necessary values instead of storing it in temp file – Madhan Jun 24 '15 at 18:00

1 Answers1

0

Try to replace your code with the following,if you have multiple keys and values make a loop to add them. Also make sure you are putting the right path of your properties file in FileWriter's constructor.

 BufferedWriter bw = new BufferedWriter(new FileWriter("yourFile.properties", true));
 bw.write(key + "=" + value);
 bw.newLine();
 bw.flush();

propertie file is nothing but a plain text file, really your question could be convert to "How do I add the text at the end of the file"

Jegg
  • 549
  • 3
  • 11