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

File.deleteOnExit() does not work on open files (win32)

    Details

    • Subcomponent:
    • CPU:
      generic, x86
    • OS:
      generic, windows_98, windows_nt, windows_2000

      Description

      If the File.deleteOnExit method is invoked on a File object, and one or more
      instances of FileInputStream, FileOutputStream, or RandomAccessFile are open on
      the file at the time the VM exits, then the file will not be deleted. This
      occurs only on win32, which does not allow a file to be deleted until all
      streams on it have been closed. -- mr@eng 9/3/1998


      Name: skT45625 Date: 06/09/2000


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


      In the code below, the line that is commented-out causes the temporary file to
      not be deleted on JVM exit. If the line is uncommented, the file does get
      deleted.

      Possibly the garbage collection for a RandomAccessFile should close the file
      just in case it was not closed. If Java had destructors, the destructor for
      this class would make sure to close the RandomAccessFile.


      ----------
      import java.io.*;

      public class DeleteTest {

      private static String FILE_PREFIX = "inet";
      private File tempFile;
      private RandomAccessFile raf;

      public DeleteTest() {
      try {

      tempFile = File.createTempFile(FILE_PREFIX, null);
      System.out.println("TEMPFILE = " +
      tempFile.getAbsolutePath());
      raf = new RandomAccessFile(tempFile, "rw");

      tempFile.deleteOnExit();

      DbByteArrayOutputStream bout = new
      DbByteArrayOutputStream();
      ObjectOutputStream out = new ObjectOutputStream(bout);

      String testStr = new String("TEST STRING");
      out.writeObject(testStr);
      out.flush();

      bout.writeTo(raf);
      bout.close();

      //raf.close();

      } catch (Exception e) {
      e.printStackTrace();
      }
      }


      public static void main(String args[]) {
      DeleteTest app = new DeleteTest();
      }


      public class DbByteArrayOutputStream extends ByteArrayOutputStream {

      public DbByteArrayOutputStream() {
      super();
      }

      public DbByteArrayOutputStream(int size) {
      super(size);
      }

      public synchronized void writeTo(DataOutput dstr) throws
      IOException {
      byte[] data = super.buf;
      int len = super.size();
      dstr.write(data, 0, len);
      }

      }
      }
      (Review ID: 105968)
      ======================================================================

        Attachments

          Issue Links

            Activity

              People

              • Assignee:
                alanb Alan Bateman
                Reporter:
                mr Mark Reinhold
              • Votes:
                0 Vote for this issue
                Watchers:
                2 Start watching this issue

                Dates

                • Created:
                  Updated:
                  Resolved:
                  Imported:
                  Indexed: