CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    Feb 2001
    Posts
    25

    Detect EOF in readLine using LineNumberReader/BufferedReader

    Dear all,

    I wonder if it is possible to check in readLine operation that the line read is the last line.

    In my program, I repeatedly Read data into my own object (construct new object, read data then put it into a vector). null pointer exception occurred when I tried to read past the end of file.
    Is it possible to check (before readLine()) operation that EOF is reached?


  2. #2
    dlorde is offline Elite Member Power Poster
    Join Date
    Aug 1999
    Location
    UK
    Posts
    10,163

    Re: Detect EOF in readLine using LineNumberReader/BufferedReader

    It isn't clear from your message whether you're using LineNumberReader.read() or LineNumberReader.readLine(). LineNumberReader.readLine() returns null when EOF is reached, so if you test the returned String before trying to use it, you shouldn't get a null pointer exception. Without seeing the relevant code, I can't really say more. This is the kind of thing:FileReader fr = new FileReader("MyFile.txt");
    LineNumberReader lnr = new LineNumberReader(fr);
    String line = lnr.readLine();
    while (line != null) {
    ... // do something with line
    line = lnr.readLine();
    }

    If you're using LineNumberReader.read() you should test the returned int for -1 (EOF).

    Dave

    To email me remove '_spamjam' from my email address
    Please use [CODE]...your code here...[/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.

  3. #3
    Join Date
    Nov 2002
    Posts
    16
    How would you detect an EOF with BufferedReader though?

    I am reading a config file where I have empty lines, so i can't use the usual "while (line.readLine() != null)" bit.

    Sample of config file:
    #
    # myapp.cfg
    #
    # Date: 3/25/03
    #

    # The name of the log file
    LogFile = /logs/myapp.log

  4. #4
    dlorde is offline Elite Member Power Poster
    Join Date
    Aug 1999
    Location
    UK
    Posts
    10,163
    Ron - why not just try it and see?

    BufferedReader returns null when EOF and an empty String ("") when a line with only a line terminator is found.

    Works for me.

    The empirical approach resolves all doubt...
    Please use [CODE]...your code here...[/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.

  5. #5
    Join Date
    Nov 2002
    Posts
    16
    my bad.. sorry.

  6. #6
    Join Date
    May 2011
    Posts
    1

    Re: Detect EOF in readLine using LineNumberReader/BufferedReader

    this will detect EOF before you read the last line:

    int readAheadLimit = 1024;
    boolean endOfFile = false;
    reader.mark(readAheadLimit);
    String tmp = reader.readLine();
    if(tmp==null) endOfFile = true;
    reader.reset();

  7. #7
    dlorde is offline Elite Member Power Poster
    Join Date
    Aug 1999
    Location
    UK
    Posts
    10,163

    Re: Detect EOF in readLine using LineNumberReader/BufferedReader

    Quote Originally Posted by jeffzfcheng View Post
    this will detect EOF before you read the last line...
    You revived an 8 year old thread to give an unnecessarily convoluted solution to a problem that was solved more simply 8 years ago?

    Why?

    Experience is a poor teacher: it gives its tests before it teaches its lessons...
    Anon.
    Please use [CODE]...your code here...[/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured