Reading a text file from a JSP page - i keep getting a java.io.FileNotFoundException
2 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   elie_medeiros
Posted On:   Sunday, August 11, 2002 06:08 PM

hi there, I am trying to read a text file from a test.jsp JSP page on my server. The text file sits in the same directory, but Tomcat keeps telling me that there is a java.io.FileNotFoundException. Does anyone have any idea why that is? my code is as follows: <% class ReadSource { public String line[]; public String text="hello="; public String ReadSource (){ try { FileReader file = new FileReader("test.txt"); BufferedReader buff = new BufferedReader(file); boolean eof = false; int i=0; while (!eof) {    More>>

hi there,




I am trying to read a text file from a test.jsp JSP page on my server. The text file sits in the same directory, but Tomcat keeps telling me that there is a java.io.FileNotFoundException. Does anyone have any idea why that is?



my code is as follows:



<%



class ReadSource {

public String line[];

public String text="hello=";




public String ReadSource (){



try {

FileReader file = new FileReader("test.txt");

BufferedReader buff = new BufferedReader(file);

boolean eof = false;

int i=0;


while (!eof) {

i++;

line[i]=buff.readLine();

if (line[i] == null)

{

eof = true;


}

else {

eof=false;

}

}

//System.out.println(line);


buff.close();

int j=0;



while (j <=i){

j++;

text.concat(line[j]);


}





} catch (IOException e) {

System.out.println ("Error -- " + e.toString());

}

return text;


}

}

%>





This doesn't seem to work so I also tried this:



class test {



public String test(){


InputStream fis = new FileInputStream("test.txt");



String data = "";



do{

int i = fis.read();

if (i != -1) data = data + (char)i;

}while(i != -1);




return data;

}

}







I try launching the code as follows:





ReadSource l=new ReadSource();



test meTest= new test();






%>

First echo l:

<%=l%>


Now echo the text:

<%=l.text%>


Now echo the lines:

<%=l.line%>



Now echo data:

<%=data%>

<%@ page import = "java.io.*" %>





At this point, all of the code sits in the same .jsp page.

Also, neither the System.out.println or just out.println seem to work- System.out.println doesn't seem to do anything even though the Tomcat terminal window is open, and out.println returns an error... any ideas about this also?



Thanks a lot for your help,




Elie

   <<Less

Re: Reading a text file from a JSP page - i keep getting a java.io.FileNotFoundException

Posted By:   rishi_mohta  
Posted On:   Monday, August 12, 2002 09:23 PM


1.The code creates an inner class and you can not access a non-final variable "out" .

2. public String ReadSource() is a method, not a constructor . you have to call it after creating instance of your class.

3.initialize line[] to avoid NulllpointerException



regards,
rishi

Re: Reading a text file from a JSP page - i keep getting a java.io.FileNotFoundException

Posted By:   Vladislav_Kamensky  
Posted On:   Monday, August 12, 2002 04:05 AM

Point is that

FileReader file = new FileReader("test.txt");


Is not referred to the same directory where jsp file is.

I think you should use the following method:


ServletContext#getRealPath(java.lang.String path)


Returns a String containing the real path for a given virtual path. For example, the path "/index.html" returns the absolute file path on the server's filesystem.

Result of this method you should use as parameter for FileReader.
About | Sitemap | Contact