IBM developerWorks
           Java Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming LanguagesJava Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
Be the architects of evolution and help create the mobile internet future. It’s your move---enter to win here!
  #1  
Old August 11th, 2002, 07:54 PM
zcrar70's Avatar
zcrar70 zcrar70 is offline
Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2000
Posts: 49 zcrar70 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 9
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

Reply With Quote
  #2  
Old August 11th, 2002, 10:57 PM
bricker42 bricker42 is offline
Moderator =(8^(|)
Dev Shed Intermediate (1500 - 1999 posts)
 
Join Date: Feb 2002
Location: Sacramento, CA
Posts: 1,710 bricker42 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 20 m 38 sec
Reputation Power: 8
Send a message via AIM to bricker42
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".

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesJava Help > relative newbie - read file into string from JSP page?


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 1 hosted by Hostway