File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes JSP and the fly likes Reading from a file Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSP
Bookmark "Reading from a file" Watch "Reading from a file" New topic

Reading from a file

Andy Patoel

Joined: Jul 09, 2004
Posts: 3
I have a JSP file that creates a Java object, and that Java object attempts to open a file using FileReader to get some data out of it. However, when I input the filename( FileReader myFileReader = new FileReader( "myFile.txt" ); // not my usual naming conventions, just an example ), I'm getting a File Not Found error. Where should the file I want to read (myFile.txt, in this example) be placed on the server (which directory) so that my Java back-end can read it?


Kalai Selvan
Ranch Hand

Joined: Jul 07, 2004
Posts: 79
U can keep the *.txt file in the same directory as tht of ur *.jsp page.

Or if u keep the text file in some other directory. U can use code something like this....

Here strRealPath will contain the fully qualified path e.g. D:/Tomcat/webapps/myProj/WEB-INF/somedir/filename.txt.

Kalai Selvan T.
Bear Bibeault
Author and ninkuma

Joined: Jan 10, 2002
Posts: 63838


JavaRanch is a community of people from all over the world, many of who are not native English speakers. While using abbreviations like "u" instead of spelling out "you" is convenient when text messaging your friends on a cell phone or in a chat room, it presents an extra challenge to those that are already struggling with English. Additionally, such shortcuts may confound automated translation tools that patrons of the Ranch may be making use of.

I would like to ask for your help in making the content of JavaRanch a little easier to read for everybody that visits here by not using such abbreviations.

JSP Forum Bartender

[Asking smart questions] [About Bear] [Books by Bear]
Bear Bibeault
Author and ninkuma

Joined: Jan 10, 2002
Posts: 63838

While the ServletContext.getRealPath() method is the correct way to locate a file within your application, mixing the file in with your JSPs may not be the best organization. Frequently such files are kept in a folder under WEB-INF. Configuration files for example, I usually put in a folder such as WEB-INF/config.
I agree. Here's the link:
subject: Reading from a file
It's not a secret anymore!