• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

can't read data file in jsp using BufferedReader

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am using following code to read BuildingInfo.txt file in JSP.The file is in the current directory. But every time I get the error java.io.FileNotFoundException: ./BuildingInfo.txt (No such file or directory)
<%
try {
BufferedReader in
= new BufferedReader(new FileReader ("BuildingInfo.txt"));
out.print("working good");
}
catch(Exception e){
out.print("Exception :"+e.toString());
}
%>

can anybody help me regarding this?
 
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well - the exception says quite clearly what happens: "No such file or directory".
I guess you placed a BuildingInfo.txt somewhere, but not where it is expected.
Since you did not tell us - let me guess: You placed it in the webapps home directory.
But the JSP seems to search somewhere else. Hmm how can we find out where the JSP is looking for it?
Just let's simply turn it the other way round and let the JSP write some code. Then we will see which directory is used.
So add a few lines in front of your existing code:
<%try {
FileWriter writer = new FileWriter("BuildingInfo.txt");
writer.write("here it is");
writer.close();
}
catch(Exception e){
out.print("Exception :"+e.toString());
}%>
You can add this in front of your existing code and you will see a "working good" in your browser window.
Why is this? Because the file has been written to the place where the JSP expects it. So your other code now can find it (of course not the one you wanted to read, but the one containing the "here it is" which has been created before).
Now you can use the find command of your OS to locate it.
Let's assume you use Windows you will find it in C:\
This is not very surprising, because you did not name any directory so the VM uses the root directory of your OS.
You need to give the full path if you want to read/write from a particular place in the filesystem.
sl
Hartmut
 
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The statement

looks in the "current directory". In the servlet/JSP environment, you have no control over what the "current directory" is.
Your alternatives are to use an absolute file path as Hartmut says or use a path relative to the "web application" location with something like:

Bill
 
Pravin S
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks bill.
<%
try {
FileWriter writer = new FileWriter("\\\\myServer\\jsp_test\\doc\\data\\BuildingInfo.txt");
writer.write("here it is");
writer.close();
out.print("GOOD GOING1");
//FileReader writer = new FileReader("\\\\myServer\\jsp_test\\doc\\data\\BuildingInfo.txt");
//out.print("GOOD GOING2");
}
catch(Exception e){
out.print("Exception :"+e.toString());
}
%>
THe above code works fine, but it doesn't creates a new file BuildingInfo.txt.
If I remove the comments in the above code, for FileReader my code does not works.
Do I need to enable file handling in some jsp tag?
currently I am using contentType="text/html" as follows,

<%@ page import="java.io.*,java.util.*" contentType="text/html" %>
Should I add sothing more in page tad or any other tag?
 
Hartmut Ludwig
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, Pravin,

Do I need to enable file handling in some jsp tag? [...]
Should I add sothing more in page tad or any other tag?


Nope. You just made a mistake in your code.
Read carefully over the line:
FileReader writer = new FileReader("\\\\myServer\\jsp_test\\doc\\data\\BuildingInfo.txt");
Do you see it? You try to define a previously defined variable (writer). This won't work.
Use the old code you posted here and add the code I wrote just in front of it.
sl
Hartmut
 
He was giving me directions and I was powerless to resist. I cannot resist this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic