52

Why file.mkdir is returning false?

Google indicates that there could be several reasons (e.g. security, permissions, pathname, etc).

My questions:

  1. How to find the exact reason of returning false?
  2. If security/permissions is a reason, then why is SecurityException not thrown?
Sandeep Jindal
  • 14,510
  • 18
  • 83
  • 121

5 Answers5

47

mkdir and mkdirs return false if the directory already exists, so that might be one reason for the failure.

If you are using Java 7, you can use the Files class. It throws an IOException on error with pretty good descriptions.

Files.createDirectory(file.toPath());
Michael Krussel
  • 2,586
  • 1
  • 14
  • 16
42

If security/permissions is a reason, then why is SecurityException NOT thrown (which is mentioned in javadoc)?

A SecurityException is thrown when you don't have JVM-level permission to do something, not OS-level

Is there a way to find the exact reason why of returning false?

No, 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:

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:

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.

This is true for most of the methods in Files, and so using it is recommended over using the methods in the File class.

Brian
  • 17,079
  • 6
  • 43
  • 66
  • 1
    With "mkdir", if you are trying to mkdir("/path1/path2/"); and the path1 (parent) folder not existed before you execute the command, the mkdir will fail, it can only create a new folder inside an existed folder, mkdirs can create all folders - path1 and path2. I have no clue with the failing of mkdirs. Hope it help – Andiana May 05 '17 at 08:53
  • @Andiana Correct. That's because Java's `File.mkdir` follows Linux conventions instead of Windows conventions, and it doesn't create intermediate directories. `File.mkdirs` follows Windows conventions, and it behaves like `mkdir --parents` in Linux. Good point. Still, for getting the exact reason, Michael's answer below is the best one: use Java's NIO packages. – Brian May 05 '17 at 17:26
7
  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.

  2. A SecurityException is thrown if there is a security violation in the SecurityManager's checkRead() and checkWrite() methods. 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. However, calling File.mkdirs() will create the non-existent parent directories.

Jon Lin
  • 142,182
  • 29
  • 220
  • 220
3

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 (e.g. results\results_ddMMyyyy without first creating the results directory) on Windows.

However, the exact same code worked fine on my Mac, i.e. no such exception was thrown on Mac and the intermediate results directory was created implicitly by the file.mkdir() method.

Hope this helps someone in future.

Akshay Maldhure
  • 787
  • 2
  • 19
  • 38
  • This is actually similar to my case, I have a legacy tool written in java and executed as a jar file in a Linux VM, when I tried to run it on Windows, one of the errors related to the code being too OS specific is the nested directory situation. – Amine Aug 15 '22 at 21:51
0

My problem was that I didn't include "/" before the path I gave which made my path wrong and hence failure.

What I was doing

"Talkify/Media/Picture/Sent"

What it should look like

"/Talkify/Media/Picture/Sent"
Exitron
  • 17
  • 12