Why file.mkdir is returning false?
Google indicates that there could be several reasons (e.g. security, permissions, pathname, etc).
My questions:
- How to find the exact reason of returning false?
- If security/permissions is a reason, then why is SecurityException not thrown?
A
SecurityExceptionis thrown when you don’t have JVM-level permission to do something, not OS-levelNo, 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. Specifically,
Files.createDirectory:If you want to use NIO entirely without using
java.io.File, you can usePaths.getto create aPathinstead:In both cases, if the directory can’t be created, it will throw an
IOExceptionwith an exact reason for why the operation failed.This is true for most of the methods in
Files, and so using it is recommended over using the methods in theFileclass.