Java Files.createDirectory() : FileAlreadyExistsException
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16179102/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Files.createDirectory() : FileAlreadyExistsException
提问by Chris Watts
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"
我在使用 Java 7 的Files
类时遇到了一个看似奇怪的问题。我想确保我的目录和存在的文件之前,我开始写,以避免FileNotFoundException
,并根据Javadoc中,createDirectory
用于检查“文件的存在和目录的创建,如果它不存在”
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)
采纳答案by Mathter
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."
“通过首先创建所有不存在的父目录来创建目录。与 createDirectory 方法不同,如果由于目录已经存在而无法创建目录,则不会引发异常。”
Maybe you could use that one
也许你可以用那个
回答by Pradeep Pati
Java 7 documentationalready mentions that you would get a FileAlreadyExistsException
. So what seems to be the problem?
Java 7文档已经提到你会得到一个FileAlreadyExistsException
. 那么问题出在哪里呢?
回答by drewich
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."
Files.createDirectory 实际上创建了目录 ->“创建一个新目录。.....createDirectories 方法应该用于需要首先创建所有不存在的父目录的地方。”
If you want to make sure the file exists, just use file.exists() method
如果要确保文件存在,只需使用 file.exists() 方法
回答by PradeeP AgrawaL
if (!Files.isDirectory(Paths.get(dirpath))) {
System.out.println("Output Files parent path does not exist:"+dirpath);
File file = new File(dirpath);
if (!file.exists()) {
if (file.mkdir()) {
System.out.println("Output files directory is created!");
} else {
System.out.println("Failed to create output directory!");
}
}
}
first check then create!!
先检查再创建!!
回答by raisercostin
I used public static Path createDirectories(Path dir, FileAttribute<?>... attrs) throws IOException
and still got an java.nio.file.FileAlreadyExistsException
because one of the existing files was a symlink. The method is not very reliable.
我使用过public static Path createDirectories(Path dir, FileAttribute<?>... attrs) throws IOException
并且仍然得到一个,java.nio.file.FileAlreadyExistsException
因为现有文件之一是符号链接。该方法不是很可靠。