Files.createDirectory() : FileAlreadyExistsException
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
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, 2022Comments
-
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 aFileNotFoundException
, 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 about 11 yearsThat's exactly what I want to do, create all nonexistent directories.
-
Chris Watts about 11 yearsSo 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 about 11 yearsYep, that works - but now
createFile
throws the exception; is there a similar thing for that? There's no such method ascreateFiles
. -
Pradeep Pati about 11 yearsNo, 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 about 11 yearsI misunderstood the document - I thought it wouldn't care.
-
Chris Watts about 11 yearsScrew this, I'm just going to do it the old way with file.exists()
-
granadaCoder over 3 yearsAnswer is much appreciated. But is today's "java are you kidding me?" moment. (originally trying to use createDirectory)
-
Tech Expert Wizard over 3 yearsSomeone 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 aFileAlreadyExistsException
if it finds that the directory already exists. Although the Javadoc did sayFileAlreadyExistsException - 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 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—including file extension—or if the directory already exists.