1

I am able to load property file using InputStream but after modifying not able to update .

FileOutputStream output = 
            new FileOutputStream("/src/main/resources/test.properties");
props.store(output, "Last Time the file have been modified at:");

How to update ? any suggestion...

VirtualTroll
  • 2,859
  • 1
  • 25
  • 42
user1788115
  • 81
  • 4
  • 12
  • Its already asked just go through this you will have an idea http://stackoverflow.com/questions/7202765/how-to-update-a-property-file – Sundar G Apr 15 '13 at 12:33
  • Here is the sample for updating the property file http://www.drdobbs.com/jvm/readwrite-properties-files-in-java/231000005 – Sundar G Apr 15 '13 at 12:35
  • 2
    Before continuing to post incomplete, unhelpful and overly generic answers (e.g. "Use Commons Configuration, blah blah" *without* explaining in any way how exactly it helps OP's concrete problem) in an attempt to collect some easy reputation, please be aware that OP is developing a Java EE web application and that the OP is essentially attempting to manipulate files in the deploy space (which he currently incorrectly assumes to be the IDE's project folder). – BalusC Apr 15 '13 at 12:39
  • Can this help : `FileOutputStream fos = new FileOutputStream (getServletContext().getRealPath("actual path under WEB-INF"));` ? – NINCOMPOOP Apr 15 '13 at 12:50
  • You'd best not place the properties file inside the deployment archive if it needs to be updated. – Deepak Bala Apr 15 '13 at 12:54

1 Answers1

1

It is difficult to be sure what you actually mean here, but it looks to me like you want your webapp to update a properties file that it read using getResource or getResourceAsStream. This could be difficult to impossible, and it is definitely a bad idea.

The best idea would be to store the properties in tables in the webapp's database. But if you don't have a database, then you could store the properties file somewhere in the file system ... provided this is allowed.

The only file system that is guaranteed to exist and to be writable to a webapp is its temporary filespace: https://stackoverflow.com/a/2663250/139985. But of course if you get to choose and configure the web container and / or the host operating system, you could find other (container / OS specific) places to store state that is less temporary. The catch is that your solution is liable to be non-portable.

Community
  • 1
  • 1
Stephen C
  • 632,615
  • 86
  • 730
  • 1,096
  • Could you elaborate why is it a bad idea to use `getResourceAsStream`? Is it a bad idea due to its read-only capability? – acdcjunior Apr 15 '13 at 13:17
  • 1
    I didn't say it was bad to use `getResourceAsStream`. I said it was a bad idea to try to update it. And that's because the place (file, JAR, whatever) it is being read from is possibly read only ... and your webapp has no way of knowing. – Stephen C Apr 15 '13 at 13:25