Results 1 to 6 of 6

Thread: End of file

  1. #1
    Senior Member
    Join Date
    Dec 2001
    Posts
    134

    End of file

    Hello Guys.
    I have written the code to scann display every line of the text file, but the problem i that I do not know how do we do the End Of File in Java, Na i am using "null", therefore it becomes the endless loop.
    Any sort of information will be appriciated.
    the following is the code.
    -------------------------------------
    import java.io.*;
    class ReadOne
    {
    public static void main(String[] args)throws IOException
    {


    FileReader f1 = new FileReader("E:\\Harbir\\ep04ext.txt");
    BufferedReader file = new BufferedReader(f1);


    String text;
    int i=0;



    while(file.readLine()!= null)
    {
    i++; // counting the number of lines
    text=file.readLine();
    System.out.println("\n "+text);
    }
    System.out.println("\n The value of I is: "+i);

    }
    }
    --------------------------------------------------------------
    U get What U pay for.

  2. #2
    Elite Hacker
    Join Date
    Mar 2003
    Posts
    1,407
    try while(file.readLine() != -1)
    I think that might be it. I thought there was a constant for EOF though somewhere.

  3. #3
    in C it is:

    Code:
    char c;
    while(c!=EOF)
    i think it might be the same in java too, but i'm not sure though

  4. #4
    Senior Member
    Join Date
    Dec 2001
    Posts
    134
    I did try putting -1 in place of null, but it gives the following message during compilation.
    ------------------------------------------------------------

    operator != cannot be applied to java.lang.String,int

    -------------------------------------------------------------
    I did EOF also but it say, EOF variable not recognized.

    I am using Java Creator, for writting these programs.
    U get What U pay for.

  5. #5
    Senior Member
    Join Date
    Aug 2003
    Posts
    1,018
    Might be overly simplistic, but it works for me:

    if (text == null) break;

    EDIT: A better idea would be to add a line at the end of your text file such as ENDFILE, so that when your program hits that, it ends. I know absolutely nothing about what you are trying to do, but it occurs to me that if for some reason there is a blank line between two lines of text, your program will terminate without reading in all the information from the file.

    Then all you would need is:
    if(text.equals("ENDFILE"), then end your application, or go on to whatever step is next.

  6. #6
    Senior Member
    Join Date
    Dec 2001
    Posts
    134
    Hello Guys, at times i really get crazy....
    Actually the original code was working well, the thing is that it is actually a long file so it was taking time, and i thought that "while" loop has become endless.
    Sorry for the botheration.
    I really appreciate all of your help and concern.
    Regards
    Harbir
    U get What U pay for.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •