Files.createDirectory() : FileAlreadyExistsException

32,835

Solution 1

From the documentation

public static Path createDirectories(Path dir, FileAttribute<?>... attrs) throws IOException

"Creates a directory by creating all nonexistent parent directories first. Unlike the createDirectory method, an exception is not thrown if the directory could not be created because it already exists."

Maybe you could use that one

Solution 2

Files.createDirectory actually creates the directory -> "Creates a new directory. .... The createDirectories method should be used where it is required to create all nonexistent parent directories first."

If you want to make sure the file exists, just use file.exists() method

Share:
32,835
Chris Watts
Author by

Chris Watts

I'm a generalist of computer science with experience in both industry and academic research. I graduated from Southampton with a First Class BSc. in Computer Science, and then further an MPhil. from Cambridge in Advanced Computer Science. My research projects at both institutions were acclaimed with awards. My previous industrial experience was at IBM Research in the prestigious Almaden Research Center designing and building, what was at the time, the smallest and cheapest high-sensitivity air pollution monitoring stations for global pollution management. My current position is as Chief Technical Officer of my own startup, NumberEight, where we use sensors in mobile devices to predict user context.

Updated on July 10, 2022

Comments

  • Chris Watts
    Chris Watts almost 2 years

    I have a seemingly strange issue using Java 7's Files class. I want to make sure my directory and files exist before I start writing to avoid a FileNotFoundException, and according to the Javadocs, createDirectory checks for "the existence of the file and the creation of the directory if it does not exist"

    So if it checks first, why do I have a problem with the following code when the directory already exists?

    private void writeFile() throws IOException {
        // Make sure parent directory and file are ready
        File file = "mydirectory/my.file";
        File parent = file.getParentFile();
        if (parent != null)
            Files.createDirectory(parent.toPath()); // Why do I get FileAlreadyExistsException? =[
        Files.createFile(file.toPath());
    
        // Do some file writing stuff!
    }
    

    I know I could just to a 'if not file exists then create' thing, but I thought the whole point of this method was to take care of all that for me!

    Exception data:

    java.nio.file.FileAlreadyExistsException: mydirectory
    at sun.nio.fs.WindowsException.translateToIOException(Unknown Source)
    at sun.nio.fs.WindowsException.rethrowAsIOException(Unknown Source)
    at sun.nio.fs.WindowsException.rethrowAsIOException(Unknown Source)
    at sun.nio.fs.WindowsFileSystemProvider.createDirectory(Unknown Source)
    at java.nio.file.Files.createDirectory(Unknown Source)
    
  • Chris Watts
    Chris Watts about 11 years
    That's exactly what I want to do, create all nonexistent directories.
  • Chris Watts
    Chris Watts about 11 years
    So it does, didn't notice. But why, when it infers from the description that it should just continue happily if the directory already exists?
  • Chris Watts
    Chris Watts about 11 years
    Yep, that works - but now createFile throws the exception; is there a similar thing for that? There's no such method as createFiles.
  • Pradeep Pati
    Pradeep Pati about 11 years
    No, there is no inference. Throwing an exception simply means it will not continue. May be you don't care, but somebody else might.
  • Chris Watts
    Chris Watts about 11 years
    I misunderstood the document - I thought it wouldn't care.
  • Chris Watts
    Chris Watts about 11 years
    Screw this, I'm just going to do it the old way with file.exists()
  • granadaCoder
    granadaCoder over 3 years
    Answer is much appreciated. But is today's "java are you kidding me?" moment. (originally trying to use createDirectory)
  • Tech Expert Wizard
    Tech Expert Wizard over 3 years
    Someone should file a Java bug report about this; it might not be exactly a bug, but at least the guys at Oracle should change the Javadoc for createDirectory() that it will throw a FileAlreadyExistsException if it finds that the directory already exists. Although the Javadoc did say FileAlreadyExistsException - if a directory could not otherwise be created because a file of that name already exists (optional specific exception) in the section about exceptions that the method throws, it was saying a "file," not a "directory," and I was confused about that until I saw this answer here.
  • Tech Expert Wizard
    Tech Expert Wizard over 3 years
    @ChrisWatts The Javadoc is actually being a bit vague; it said FileAlreadyExistsException - if a directory could not otherwise be created because a file of that name already exists (optional specific exception), but actually, the exception is thrown in two circumstances: a file with the same name&mdash;including file extension&mdash;or if the directory already exists.