0

I wish to change a property value inside a property file. I tried some ways, such as FileInputStream/FileOutputStream or Apache's library, but all of them alter the file structure.

The structure of my file is:

#[Section 1]
prop1=value1
prop2=value2

#[Section 2]
prop2=value2
prop4=value4

After executing the code, the property changes, but "section" items disappear, and file only consists of a list of unordered properties.
Is there a way to preserve the structure above?

I TRIED THESE WAYS: Updating property value in properties file without deleting other values

Community
  • 1
  • 1
Fab
  • 1,145
  • 7
  • 20
  • 40
  • `After executing the code`, what code are you using? It would be helpful if you could provide it in your question. – Linus Sep 21 '16 at 08:51
  • `FileInputStream` and `FileOutputStream` read and write exactly what you tell them to, so if the structure gets altered your application has some bugs. Can you share your code please? – BackSlash Sep 21 '16 at 08:54
  • Looks like [this](http://stackoverflow.com/questions/15337409/updating-property-value-in-properties-file-without-deleting-other-values) will be helpful for you, see the last answer from AnirbanDebnath. – mv200580 Sep 21 '16 at 08:55
  • And if all doesnt help, you only need a few lines of code to read the property file; to write it back into a new file; and only if a line matches `propertyThatNeedsUpdate = `... you write out a different line. – GhostCat Sep 21 '16 at 08:56

4 Answers4

1

I have used apache commons-configuration and it's worked fine:

PropertiesConfiguration conf = new PropertiesConfiguration("p.properties");
conf.setProperty("prop3", "newValue");
conf.save();

The whole structure does not change.

M.Amini
  • 106
  • 1
  • 6
  • Unfortunately, this doesn't work for me. I'm forced to use the approach suggested by Ironluca. I'm already using it to manage a property appearing in multiple sections. I have to use it also for "single" properties. – Fab Sep 21 '16 at 09:56
0

The general approach for changing any file is to read the contents of the file, keep the content of the file in the memory, change the content that is required and write back the entire content. If you are writing the property values directly then the sections marking would be gone. There is ofcourse one exception in the above case, if you are appending content to the end of the file. Anywhere else, you have to read the file and write the file as a whole.

Ironluca
  • 3,402
  • 4
  • 25
  • 32
0

Yes the entire file is rewritten. Also properties are chached (you can uncache them), and property files normally are resource files, on the class path, hence maybe packed inside a jar/war.

However it seems you have it writable and all, you can opt for an XML properties file. See the Properties API, with loadFromXML and storeToXML. This also is UTF-8 capable.

Joop Eggen
  • 107,315
  • 7
  • 83
  • 138
0

For example like this:

try(InputStream is = getClass().getResourceAsStream("my.properties")) {
    Properties properties = new Properties();
    properties.load(is);
    properties.setProperty("prop4", "CHANGED");
    props.store(out);
}    

Note that it is not common to write to properties files. Typically you provide a default file in your distribution and the user can alter values to configure the system. If you need to store application data, consider using the Java Preferences API.

Adriaan Koster
  • 15,870
  • 5
  • 45
  • 60