• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

getting file path in the servlet...

 
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All,
I have a servlet which reads the information from a file 'myfile.txt'.
My requirement is that I should not hard code the file path in the servlet. If that is the case wher do I set the file path, so that when the servlet is invoked it gets the file path and does the reading process.
Is there also any specific directory where I should keep the above mentioned file.
Please throw some light on the above problem.
Thanks
Vasu
 
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you put the file in the web-inf/classes directory, you can use the local ClassLoader to find the file for you.
Try something like this in your servlet:

Then you should be able to open it with a FileInputStream or whatever you want...
Dave
 
vasu devan
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi David O'Meara,
As you suggested I tried keeping the myFile.txt in WEB-INF/classes dir. But stil my servlet is not in position to resolve the particular file.
Moreover as a project requirement, the file can be in any directory,in that case where do I mentioned the path, so that my servlet opens the file and read the content.
Can any one help me in resolving the above issue.
Thanks,
Vasu.
 
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I like to use an <init-param> in the web.xml file to designate a working directory for reading and writing data. Not as portable as using WEB-INF/ but more flexible.
Bill
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic