简体   繁体   English

File.mkdir或mkdirs返回false - 原因?

[英]File.mkdir or mkdirs return false - Reason?

Why file.mkdir is returning false? 为什么file.mkdir返回false?

Google indicates that there could be several reasons (eg security, permissions, pathname, etc). Google表示可能有多种原因(例如安全性,权限,路径名等)。

My questions: 我的问题:

  1. How to find the exact reason of returning false? 如何找到返回false的确切原因?
  2. If security/permissions is a reason, then why is SecurityException not thrown? 如果安全/权限是一个原因,那么为什么不抛出SecurityException?

mkdir and mkdirs return false if the directory already exists, so that might be one reason for the failure. 如果目录已经存在,mkdir和mkdirs将返回false,这可能是失败的原因之一。

If you are using Java 7, you can use the Files class. 如果您使用的是Java 7,则可以使用Files类。 It throws an IOException on error with pretty good descriptions. 它会在错误时引发IOException并带有非常好的描述。

Files.createDirectory(file.toPath());

If security/permissions is a reason, then why is SecurityException NOT thrown (which is mentioned in javadoc)? 如果安全/权限是一个原因,那么为什么不抛出SecurityException(在javadoc中提到)?

A SecurityException is thrown when you don't have JVM-level permission to do something, not OS-level 如果您没有JVM级别的权限来执行某些操作而不是操作系统级别,则抛出SecurityException

Is there a way to find the exact reason why of returning false? 有没有办法找到返回false的确切原因?

No, AFAIK. 不,AFAIK。 The only way to know would be to check the permissions on the directory yourself, make sure it doesn't exist before calling them, check if the parent directory exists, etc. 唯一知道的方法是自己检查目录的权限,确保在调用之前它不存在,检查父目录是否存在等。

However, if you're using Java 7 or higher , you can use NIO instead to create the directory. 但是,如果您使用的是Java 7或更高版本 ,则可以使用NIO来创建目录。 Specifically, Files.createDirectory : 具体来说, Files.createDirectory

File dir = new File("mydir");
Files.createDirectory(dir.toPath());

If you want to use NIO entirely without using java.io.File , you can use Paths.get to create a Path instead: 如果要在不使用java.io.File情况下完全使用NIO,则可以使用Paths.get来创建Path

Path dir = Paths.get("mydir");
Files.createDirectory(dir);

In both cases, if the directory can't be created, it will throw an IOException with an exact reason for why the operation failed. 在这两种情况下,如果无法创建目录,它将抛出IOException其中包含操作失败原因的确切原因。

This is true for most of the methods in Files , and so using it is recommended over using the methods in the File class. 对于Files大多数方法都是如此,因此建议使用它而不是使用File类中的方法。

  1. No, there's no way to find the exact reason mkdirs() returns false, at least not from within Java, as it would probably be OS dependent. 不,没有办法找到mkdirs()返回false的确切原因,至少不是来自Java内部,因为它可能依赖于操作系统。

  2. A SecurityException is thrown if there is a security violation in the SecurityManager 's checkRead() and checkWrite() methods. 一个SecurityException如果有一个安全冲突中被抛出SecurityManagercheckRead()checkWrite()方法。 The exception isn't thrown if there is an OS permissions issue. 如果存在操作系统权限问题,则不会抛出异常。


Additionally, note that if you call File.mkdir() , and the parent directory doesn't exist, mkdir() will return false. 另外,请注意,如果调用File.mkdir() ,并且父目录不存在,则mkdir()将返回false。 However, calling File.mkdirs() will create the non-existent parent directories. 但是,调用File.mkdirs()将创建不存在的父目录。

Here's something specific to Windows: In my case, the file.mkdir() method was failing with NoSuchFileException because I was trying to create a nested directory structure directly (eg results\\results_ddMMyyyy without first creating the results directory) on Windows. 这是Windows特有的东西:在我的例子中, file.mkdir()方法失败了NoSuchFileException因为我试图在Windows上直接创建一个嵌套的目录结构(例如results\\results_ddMMyyyy而不首先创建results目录)。

However, the exact same code worked fine on my Mac, ie no such exception was thrown on Mac and the intermediate results directory was created implicitly by the file.mkdir() method. 但是,完全相同的代码在我的Mac上工作正常,即没有在Mac上抛出此类异常,并且中间results目录是由file.mkdir()方法隐式创建的。

Hope this helps someone in future. 希望这有助于将来的某些人。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM