Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

why BufferedReader.readLine can read a line which doesn't have a line separator

Tags:

java

Reads a line of text. A line is considered to be terminated by any one of a line feed ('\n'), a carriage return ('\r'), or a carriage return followed immediately by a linefeed.------javadoc 1.8

Then I have a text file like this:

the first line
the second line

note: the last character of the seond line is 'e' that is to say there dont exist carriage return.

then here is my demo code.

public void process() throws IOException{
    BufferedReader br = new BufferedReader(new FileReader("demo.txt"));
    String line;
    while((line=br.readLine())!=null){
        System.out.println(line);
    }
    br.close();
}

the real output:

 the first line
 the second line

then my question is that why the readLine method can get the second line for it doesnt have line-separator (\n or \r or \n\r).
I know there exist a end of file (EOF), but it seemed the javadoc dont tell the EOF is also the line-separator explicitly.

If I use Scanner instead of BufferedReader the code as below:

public void testScan() throws IOException{
    Scanner scan = new Scanner(new FileInputStream("demo.txt"));
    String line;
    while((line=scan.nextLine())!=null){
        System.out.println(line);
    }
    scan.close();
}

then the output would be:

the first line
the second line
Exception in thread "main" java.util.NoSuchElementException: No line found
    at java.util.Scanner.nextLine(Scanner.java:1540)
    at com.demo.Demo.testScan(Demo.java:39)
    at com.demo.Demo.main(Demo.java:49)
like image 641
nail fei Avatar asked Mar 20 '17 06:03

nail fei


People also ask

Does BufferedReader read line by line?

Class BufferedReader. Reads text from a character-input stream, buffering characters so as to provide for the efficient reading of characters, arrays, and lines. The buffer size may be specified, or the default size may be used. The default is large enough for most purposes.

What happens if BufferedReader's readLine () method has nothing more to read?

If there are no more lines to read, bf. readLine()returns null.

Which method is used for reading a line of text in BufferedReader?

The readLine() method of BufferedReader class in Java is used to read one line text at a time.

What does readLine return for an empty line?

readLine() returns an empty string if the line is empty. The javadoc says: Returns: A String containing the contents of the line, not including any line-termination characters, or null if the end of the stream has been reached.


1 Answers

Because it's programmed that way.

Really, it's what the user of the method wants. If the last line is missing a line separator at the end, it will read until EOF so that no data is lost. You don't want to lose an entire line because of a missing line separator.

Practically all similar functions work in the same way. For example, if you're looking at the fgets() function in the C library, it will also work that way. So does f.readline() in Python.

Edit: the Scanner works also in the similar way, but the difference is that a Scanner throws an exception whereas BufferedReader returns null when all lines have been read.

like image 175
juhist Avatar answered Oct 02 '22 14:10

juhist