• 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:
  • Campbell Ritchie
  • Devaka Cooray
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Jeanne Boyarsky
  • Tim Cooke
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Mikalai Zaikin
  • Carey Brown
Bartenders:

Where to save the text file when reading from a servlet

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi friends ,
I am devloping a application which requires a servlet to read text file.I have written the code and it si not showing any error .But the problem when i start my Tomcat server then it shows the text file not found error and automaticaaly shutsdown.I have stored the text file in \webapps\ROOT\WEB-INF\classes directory.plz help me
Thanking all in advance
Ullas
 
Ranch Hand
Posts: 1514
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The error is self explanatory. It's just not finding the file. Post the code you are using to access the file.
 
Ullas Madathil
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
public class GetPageServer extends HttpServlet
{
protected void doGet(HttpServletRequest req, HttpServletResponse res)throws ServletException, IOException
{
BufferedReader textreader = new BufferedReader(new FileReader(textfile));
FileOutputStream exstufile = new FileOutputStream(new File("STU.txt"));
 
Sheriff
Posts: 13411
Firefox Browser VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Because a servlet app can be deployed from either a file system structure or a war file, there is no guarantee that your file will exist on the systems hard drive as a 'file'.

The ServletContext.getResource and ServletContext.getResourceAsStream methods are provided for just this reason. If you use one of these methods, you will be able to refer to your file, relative to the context root of the app.

http://java.sun.com/j2ee/1.4/docs/api/javax/servlet/ServletContext.html
 
Sheriff
Posts: 67735
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"Ullas V M",

There aren't many rules that you need to worry about here on the Ranch, but one that we take very seriously regards the use of proper names. Please take a look at the JavaRanch Naming Policy and adjust your display name to match it.

In particular, your display name must be a first and a last name separated by a space character, and must not be obviously fictitious.

Thanks!
bear
JavaRanch Sheriff
 
What do you have in that there bucket? It wouldn't be a tiny ad by any chance ...
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic