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

RandomAccessFile.close method does not close associated channel

XMLWordPrintable

    • mantis
    • sparc
    • solaris_2.6
    • Verified



      Name: nkR10003 Date: 07/03/2002



      The JavaDoc comments for RandomAccessFile.close() method says:
      /**
       * ...
       *
       * If this file has an associated channel then the channel is closed as well.
       *
       * ...
       *
       */

      This statement does not work.

      Example below demonstrates this problem:
      ------------------example--------------------
      import java.io.*;
      import java.nio.channels.*;

      public class test {
          public static void main(String[] args) {
              File f = new File("test.txt");
              try {
                  f.createNewFile();
                  f.deleteOnExit();
                  RandomAccessFile raf = new RandomAccessFile(f, "r");
                  FileChannel channel = raf.getChannel();
                  raf.close();
                  if (channel.isOpen()) {
                      System.out.println("Test failed: channel is open");
                      System.exit(-1);
                  }
              } catch (IOException e) {
                  System.out.println("Test failed: unexpected exception: " + e);
              }
              
              System.out.println("Test passed");
              return;
          }
      }
      ----------------output:----------------------
      %java -version

      java version "1.4.1-rc"
      Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.1-rc-b16)
      Java HotSpot(TM) Client VM (build 1.4.1-rc-b16, mixed mode)

      %java test

      Test failed: channel is open
      ---------------------------------------------
      ======================================================================

            mmcclosksunw Michael Mccloskey (Inactive)
            knasunw Kna Kna (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: