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

File.createTempFile() throws IOException "Access is denied"

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Fixed
    • Icon: P4 P4
    • 8
    • 5.0u4
    • core-libs
    • other
    • windows_xp

      OPERATING SYSTEM(S)
      Windows XP
      FULL JDK VERSION(S):
      java version "1.5.0_04"
      Java(TM) 2 Runtime Environment, Standard Edition (build 1.5.0_04-b05)
      Java HotSpot(TM) Client VM (build 1.5.0_04-b05, mixed mode)
      DESCRIPTION:
      When running the following testcase, the createTempFile call occassionally throws
      an IOException "Access is denied".
      import java.io.File;
      public class TestFile {
          public static void main(String[] args) throws Exception {
              for (int i = 0; i < 5; i++) {
                  File tempFile = File.createTempFile("WSTMPCC", null, null);
                  System.out.println("*** create = " + tempFile.getAbsolutePath() + " ***");
                  tempFile.delete();
                  System.out.println("*** delete = " + tempFile.getAbsolutePath() + " ***");
                  tempFile.mkdir();
                  System.out.println("*** mkdir = " + tempFile.getAbsolutePath() + " ***");
                  tempFile.mkdir();
              }
          }
      }
      C:\dev\test>java TestFile
      Exception in thread "main" java.io.IOException: Access is denied
              at java.io.WinNTFileSystem.createFileExclusively(Native Method)
              at java.io.File.checkAndCreate(File.java:1345)
              at java.io.File.createTempFile(File.java:1434)
              at TestFile.main(TestFile.java:8)
      What appears to be happening is that createTempFile generates a temporary filename
      and then attempts to create a new empty file with that name using the CreateFileW
      OS call. If a directory already exists with that name, CreateFileW appears to be
      returning an error of ERROR_ACCESS_DENIED, which is being treated as an error. What
      should be happening is that the class libraries should be generating a new
      temporary filename and trying again.
      Looking at the MSDN documentation for CreateFileW, I'm not certain that
      ERROR_ACCESS_DENIED cannot be thrown in other circumstances, so I do not believe
      it is safe to simply ignore that error and retry as we currently do for
      ERROR_FILE_EXISTS. Instead a test could be made before CreateFileW is called to
      test if the generated filename already exists and if it does abort such that a new
      filename can be generated and the attempt to create a new empty file repeated.
      [This bug is being submitted as a courtesy, in order to maintain uniformity between Sun & IBM JDKs. It has been fixed in IBM JDKs.
      Please contact ###@###.### if you have questions.]

            dxu Dan Xu (Inactive)
            elarsen Erik Larsen (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: