• 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
  • Ron McLeod
  • Paul Clapham
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Rob Spoor
  • Bear Bibeault
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:
  • Frits Walraven
  • Himai Minh

Reading a TXT file in a package...

 
Ranch Hand
Posts: 301
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I wasn't sure where to put this question since it seems to cross several boundaries.
I have a class in a package. The class reads a TXT file and prints it's contents out for a Servlet. The package is in the Tomcat Web-app classes directory, but no matter what I do it cannot seem to find the file.
I have tried the file in the web-app root, in the classes directory, and in the package directory but it cannot seem to find it.
I do something similar in a non-servlet environment and it works fine. In that case I have the file in the directory just above the package and it finds it just fine.
Does anyone have any ideas?
TIA
 
Joel Cochran
Ranch Hand
Posts: 301
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry to revive this but i still can't get it to work.
I've been playing with it and I'm receiving a FileNotFoundException. I have tried the filename, /filename, directory/filename, /directory/filename, and even the absolute URL, but still the class will not find the file.
I have put the file in ROOT, the web-app directory, WEB-INF, classes, and even at the package level, but NOTHING seems to find this file.
If no one knows, can anyone suggest somewhere else I might ask?
TIA,
Joel
 
Sheriff
Posts: 9103
12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So the servlet is in a package in the classes subdir of Tomcat. Is the text file also in the package or is it separate?

How did you code the call to the text file?
 
Joel Cochran
Ranch Hand
Posts: 301
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No, the servlet is in the 'classes' directory. It uses a class in a package (the package is in the 'classes' directory as well) that reads the file.
Here is the code for the class:

I copied this from another program outside of Tomcat where it works as desired.
Thanks Marilyn!
 
Marilyn de Queiroz
Sheriff
Posts: 9103
12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Try including the path in front of the file name in your call.

For example:
IncludeFileText text = new IncludeFileText();
text.include( "/Java/names.txt" );

Otherwise you'll have find a way to get the dir of the server root (Check com.javaranch.common.LocalSystem for some ideas on that).
 
Sheriff
Posts: 6920
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have found from long experience with servlet containers that this is probably the wrong way to go about it. If you load the file as a "resource" rather than as a file, you can put it in the classes directory and it will be available to anythning in your web app. I use the following code, which might help:
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic