• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Reading from a file

 
Andy Patoel
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 64994
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64994
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic