File.mkdir or mkdirs return false - Reason?

Sandeep Jindal picture Sandeep Jindal · Aug 30, 2012 · Viewed 41.3k times · Source

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?

Answer

Michael Krussel picture Michael Krussel · Aug 30, 2012

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());