aspose file tools*
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
Author

Reading from a file

Andy Patoel
Greenhorn

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?

Thanks,

Andy
Kalai Selvan
Ranch Hand

Joined: Jul 07, 2004
Posts: 79
Hi,
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
Marshal

Joined: Jan 10, 2002
Posts: 61224
    
  66

Kalai,

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.

thanks,
bear
JSP Forum Bartender


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61224
    
  66

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: http://aspose.com/file-tools
 
subject: Reading from a file