Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Accessing files that are there in classes directory

 
Raghavender Ammagari
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a properties file that is present in WEB-INF/classes...i could read that file as shown below



how to do a write process on the same file...the code till now is given below..please help me...
 
Raghavender Ammagari
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please do reply..had expected an immediate solution in coderanch
 
Stefan Evans
Bartender
Posts: 1712
10
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Writing/modifying properties files at run-time sounds like trouble to me.
Particularly one that is part of a web application classpath.
There is a reason that you can only GET resources from the classLoader.

To answer your question on how you may be able to do it:

An alternative method to look at is the ServletContext method: getRealPath().
That converts a website relative path into a true location on disk.
so


Now you have the File handle, you can open it for writing.

However
- this may not actually resolve to a File on disk. It might be a file in a jar archive, which would not be editable.
- even if it is a file, and you write to it, there is no guarantee it will be read again except by restarting the web application.

What is the use case for this? Are these config properties intended to be edited often?
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64827
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I whole-heartedly agree with Stefan that this is a bad idea. WHat happens when you need to update and redeploy the app?

I always put read/write configuration in the DB where it is independent of the application itself.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic