Relates :
|
|
Relates :
|
|
Relates :
|
|
Relates :
|
|
Relates :
|
A DESCRIPTION OF THE PROBLEM : LineNumberReader.getLineNumber() returns inconsistent results after reaching EOF depending on the methods which were called to read. readLine() always returns a line number one higher than if EOF has been reached using the other methods. ---------- BEGIN SOURCE ---------- import java.io.IOException; import java.io.LineNumberReader; import java.io.StringReader; public class EofLineNumberTest { public static void main(String[] args) throws IOException { String string = "first \n second"; /* * getLineNumber() is described as "Get the current line number." * However, the result is inconsistent after EOF */ System.out.println("Line number after EOF:"); System.out.println(" read(): " + linesRead(string)); System.out.println(" read(char[]): " + linesReadBuffer(string)); System.out.println(" readLine(): " + linesReadLine(string)); } private static int linesRead(String string) throws IOException { LineNumberReader reader = new LineNumberReader(new StringReader(string)); while (reader.read() != -1) { } return reader.getLineNumber(); } private static int linesReadBuffer(String string) throws IOException { LineNumberReader reader = new LineNumberReader(new StringReader(string)); char[] buff = new char[512]; while (reader.read(buff) != -1) { } return reader.getLineNumber(); } private static int linesReadLine(String string) throws IOException { LineNumberReader reader = new LineNumberReader(new StringReader(string)); while (reader.readLine() != null) { } return reader.getLineNumber(); } } ---------- END SOURCE ---------- FREQUENCY : always
|