|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
Be the architects of evolution and help create the mobile internet future. It’s your move---enter to win here! |
|
#1
|
||||
|
||||
|
relative newbie - read file into string from JSP page?
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%> <br>Now echo the text: <%=l.text%> <br>Now echo the lines: <%=l.line%> <br>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 |
|
#2
|
|||
|
|||
|
Well, I think your problem is most likely that when Tomcat accesses your jsp page, it compiles it into a servlet in a different directory (/work), and therefor your text file is no longer in the same directory.
The / directory in Tomcat is your application directory (ie: webapps/examples/), so try making your path "/test.txt". |
![]() |
| Viewing: Dev Shed Forums > Programming Languages > Java Help > relative newbie - read file into string from JSP page? |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|