1. Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2000
    Rep Power

    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) {
    if (line[i] == null)
    eof = true;
    else {
    int j=0;

    while (j<=i){

    } 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 = "";

    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:
    <br>Now echo the text:
    <br>Now echo the lines:
    <br>Now echo 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,

  2. #2
  3. No Profile Picture
    Moderator =(8^(|)
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Feb 2002
    Sacramento, CA
    Rep Power
    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".

IMN logo majestic logo threadwatch logo seochat tools logo