JDK-6325169 : File.createTempFile() throws IOException "Access is denied"
  • Type: Bug
  • Component: core-libs
  • Sub-Component: java.io
  • Affected Version: 5.0u4
  • Priority: P4
  • Status: Resolved
  • Resolution: Fixed
  • OS: windows_xp
  • CPU: other
  • Submitted: 2005-09-16
  • Updated: 2013-06-11
  • Resolved: 2013-06-11
The Version table provides details related to the release that this issue/RFE will be addressed.

Unresolved : Release in which this issue/RFE will be addressed.
Resolved: Release in which this issue/RFE has been resolved.
Fixed : Release in which this issue/RFE has been fixed. The release containing this fix may be available for download as an Early Access Release or a General Availability Release.

To download the current JDK release, click here.
Related Reports
Relates :  
Windows XP
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)
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() + " ***");
            System.out.println("*** delete = " + tempFile.getAbsolutePath() + " ***");
            System.out.println("*** mkdir  = " + tempFile.getAbsolutePath() + " ***");
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.]

In the fix of JDK-8013827, the new code will check whether a to-be-created temp file already exists. If not, it will proceed to create this temp file. Otherwise, it will generate a new file name and try again.

EVALUATION A while back, File.createTempFile was changed to use SecureRandom when generating the file name. One implication of this is that the chances of generating the name of an existing file or directory is now very remote. This means that the bug cited here is highly unlikely to happen.

EVALUATION When a file already exists then Windows's CreateFile checks that the attributes of the file match the attributes passed to the CreateFile. When they don't match then "access denied" is returned. The submitter noted that they fixed the issue by including a check for the file before CreateFileW is called. The details weren't included but it should it should be checked that File createNewFile is not impacted (it is spec'ed so that the check for existence of the file and the creation of the file if it does not exist are a single operation).