Name: ccC48265 Date: 02/04/98
java.io.DataInputStream and java.io.RandomAccessFile both
implement java.io.DataInput.
The documentation for java.io.RandomAccessFile.readLine() mentions:
"The line-terminating character(s), if any, are included as part of
the string returned".
When compiling and running the following test case on JDK1.1.5 for WinNT,
then for the line terminating with "\r\n",
(1) java.io.DataInputStream.readLine()
includes no line-terminating character(s),
as expected (see API doc);
(2) java.io.RandomAccessFile.readLine()
includes "\r" only, but excludes "\n".
In the latter case (2), shouldn't both "\r" and "\n" be included?
At least, that's what the documentation suggests. If this is not
a bug, then the documentation should be corrected.
--- R1.java // First line is terminated by "\r\n"
import java.io.*;
class R1 {
static public void main(String[] args) throws IOException {
String path = "R1.java";
DataInput in;
String str;
RandomAccessFile in2;
byte[] buf = new byte[20];
in = new RandomAccessFile(path, "r");
str = in.readLine();
str = str.replace('\n', 'N').replace('\r', 'R');
System.out.println("<<<" + str + ">>>"); // "...;R"
in2 = new RandomAccessFile(path, "r");
int len = in2.read(buf);
System.out.println("len = " + len);
str = new String(buf, 0, len);
str = str.replace('\n', 'N').replace('\r', 'R');
System.out.println("<<<" + str + ">>>"); // "...;RN..."
in = new DataInputStream(new FileInputStream(path));
str = in.readLine();
System.out.println("<<<" + str + ">>>"); // "...;"
}
}
============================================================
###@###.### (Feb 4, 1998):
On NT (where lines end with "\r\n"), the output of the program
is as follows:
<<<import java.io.*;R>>>
len = 20
<<<import java.io.*;RNR>>>
<<<import java.io.*;>>>
Pay attention to the first line printed; it includes the '\r'
(represented as R) only -- no '\n' (represented as N).
On Solaris (where lines end with "\n"), the output of the program
is as follows:
<<<import java.io.*;>>>
len = 20
<<<import java.io.*;NNc>>>
<<<import java.io.*;>>>
Again no '\n' can be found in the first line.
(Review ID: 24437)
======================================================================
- duplicates
-
JDK-1238814 Method readLine of class RandomAccessFile doesn't recognize \\\\\\\\r
-
- Closed
-