Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-4109353

java.io.RandomAccessFile behavior conflicts with documentation

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Duplicate
    • Icon: P3 P3
    • None
    • 1.1.5
    • core-libs
    • x86, sparc
    • solaris_2.5.1, windows_nt



      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)
      ======================================================================

            dviswanasunw Deepa Viswanathan (Inactive)
            ccresswesunw Claudette Cresswell (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: