File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Where to save the text file when reading from a servlet

 
Ullas Madathil
Greenhorn
Posts: 2
  • 0
  • Mark post as helpful
  • send pies
  • 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
 
Bosun Bello
Ranch Hand
Posts: 1510
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • 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"));
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • 0
  • Mark post as helpful
  • send pies
  • 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
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64182
83
IntelliJ IDE Java jQuery Mac Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic