JDK-4045014 : In Win95 JDK 1.1.1, File.delete() fails to delete the file from disk
  • Type: Bug
  • Component: core-libs
  • Sub-Component: java.io
  • Affected Version: 1.1.1
  • Priority: P2
  • Status: Closed
  • Resolution: Not an Issue
  • OS: windows_95
  • CPU: x86
  • Submitted: 1997-04-14
  • Updated: 1997-05-10
  • Resolved: 1997-05-10
Related Reports
Relates :  
Description
In Win95 JDK 1.1.1, File.delete() fails to delete the file from disk

Here is a reproducible test case:


import java.io.*;

public class BugReport
  {
  RandomAccessFile	tmp_file = null;

  File	file;

  public BugReport(String args[])
    {
    file = new File("foo");
    try
      {
      tmp_file = new RandomAccessFile(file, "rw");
      }
    catch (IOException e)
      {
      System.out.println("Error opening tmp file: " + e + ", exiting...");
      System.exit(0);
      }
    reopen_file();
    try
      {
      tmp_file.close();
      }
    catch (IOException e)
      {
      System.out.println("File close error: " + e + ", exiting...");
      System.exit(0);
      }
    if (!file.delete()) System.out.println("Error deleting file");
    }

  public void reopen_file()
    {
    String	data;

    try
      {
      tmp_file = new RandomAccessFile(file, "rw");
      }
    catch (IOException e)
      {
      System.out.println("Error opening tmp file");
      return;
      }
    try
      {
      tmp_file.close();
      }
    catch (IOException e){}
    }

  public static void main(String args[])
    {
    new BugReport(args);
    }
  }

To verify the bug, just create an ASCII file named "foo", add a few bytes
to it and save it. Then, in the same directory, compile and run BugReport, 
the attached Java source file. On Win 95 an error message is printed, indicating
that the file was not deleted; on Solaris, "foo" gets deleted, as it should.
If you comment out the call to reopen_file(), I believe "foo" will get deleted.


mei.chan@Corp 1997-04-24

The problem turned out to be not a bug.  We discovered a difference 
between Win95 and Solaris file system implementation.
On Solaris you can File.delete() an open file.
On Win95, you have to call tmp_file.close() before deleting the file.  
The Win95 OS keeps track of open files and would not allow you to delete the
file until you explicitly close it.

Here is the modified test case which would show the difference in behavior,
ie. File.delete() on a file without closing it:

import java.io.*;

public class BugReport
  {
  RandomAccessFile      tmp_file = null;

  File  file;

  public BugReport(String args[])
    {
    file = new File("foo");
    try
      {
      tmp_file = new RandomAccessFile(file, "rw");
      System.out.println("created the file");
      }
    catch (IOException e)
      {
      System.out.println("Error opening tmp file: " + e + ", exiting...");
      System.exit(0);
      }

    if (file.exists()) 
        System.out.println("File exists");
    else 
        System.out.println("File does not exist");

    if (!file.delete()) 
        System.out.println("Error deleting file");
    else System.out.println ("Deleted the file");
    }

  public static void main(String args[])
    {
    new BugReport(args);
    }
  }


Results on Solaris:

chicosun:/home/meiphen/java/progs/file_delete 78 % java BugReport
created the file
File exists
Deleted the file


Results on Win95:

created the file
File exists
Error deleting the file



Comments
EVALUATION Right (concurring with description). On win32 file systems, the semantics are that, if a file is currently open anywhere in the process (or, I think, by any process on the machine), the file cannot be deleted. There's no efficient way for the runtime to workaround this to provide the same semantics as a UNIX file system. The workaround is to close files before deleting them. david.a.brown@Eng 1997-05-10
10-05-1997

WORK AROUND Close the first instance of RandomAccessFile before opening and closing a second instance. sheri.good@Eng 1997-04-21 Close *the* file before deleting it! mei.chan@Corp 1997-04-24
21-04-1997