• 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
  • Paul Clapham
  • Tim Cooke
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Frank Carver
  • Henry Wong
  • Ron McLeod
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Himai Minh

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: 1512
  • 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: 67666
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
 
Don't count your weasels before they've popped. And now for a mulberry bush related tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic