• Post Reply Bookmark Topic Watch Topic
  • New Topic

How to modify a resource file  RSS feed

 
Tiberius Marius
Ranch Hand
Posts: 115
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I want from Java to modify a resource text file to locate a string , lets say date=<value> and to change the value . I have an idea of how i can read the whole file and save it in a string where i add the last line read each time. When i find the line with date i modify it and then add it to the rest of the string and then i completly rewrite the file. I will try to code this approach and post it here but would appreciate if someone can suggest a more simpler approach using a library that makes this whole thing take a few lines. Something on the side of less complex is desired.
 
Campbell Ritchie
Marshal
Posts: 56570
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do you mean you want to write an additional line in the file? That can easily be done with a buffered writer. If you pass a FileWriter, you can use a boolean argument to its constructor which changes it to append mode, rather than overwrite mode.
You usually create Properties as a sort of Map, so you can implement your options as a Map. The System class has various methods allowing you to alter the standard properties. You would have to work out what format the properties and resources are in, so you can write the additional lines in that format.
 
Stephan van Hulst
Saloon Keeper
Posts: 7992
143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, it depends on the format of your resource file. There are APIs out there that deal with INI files, XML files and .properties files, for instance.

I would dissuade you from storing your resource in a String, while it's in memory. Write a class that strongly types the properties of your resource.
 
Tiberius Marius
Ranch Hand
Posts: 115
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:Do you mean you want to write an additional line in the file? That can easily be done with a buffered writer. If you pass a FileWriter, you can use a boolean argument to its constructor which changes it to append mode, rather than overwrite mode.
You usually create Properties as a sort of Map, so you can implement your options as a Map. The System class has various methods allowing you to alter the standard properties. You would have to work out what format the properties and resources are in, so you can write the additional lines in that format.


No , i want to modify a value there aka remove something from a line and add something in it's place.
 
Campbell Ritchie
Marshal
Posts: 56570
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you put all the properties into a Map? It is very easy to alter the “V”s in a Map with the put method.
Consider copying the contents of the Map into your file when the program exits.
 
Tiberius Marius
Ranch Hand
Posts: 115
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stephan van Hulst wrote:Well, it depends on the format of your resource file. There are APIs out there that deal with INI files, XML files and .properties files, for instance.

I would dissuade you from storing your resource in a String, while it's in memory. Write a class that strongly types the properties of your resource.


It's a text file that stores some options for a different project . I cannot modify it apart from changing the values in there . I m trying to create an automation test but for that i have to first modify some values in one of the configuration files that i then move to the location where the system under test runs.
 
Paul Clapham
Sheriff
Posts: 22834
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You have to rewrite the whole file. It isn't usually possible to update part of a text file.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!