auser
auser

Reputation: 7497

How can I find out why java.io.File.mkdir() returns false

How do I find out why java.io.File.mkdir() returns false. I can create the directory manually.

UPDATE: My code looks like this:

String directoryName = "C:/some/path/";
File directory= new File(directoryName );
if (!directory.exists() && !directory.mkdir()) {
    throw new RuntimeException("Failed to create directory: " + directoryName);
}

Upvotes: 4

Views: 9695

Answers (5)

Donal Lafferty
Donal Lafferty

Reputation: 5966

Using cygwin?

mkdir may return false, but go on to create the folder anyway. The false seems only to indicate that the folder does not exist, yet.

You may have to try directory.exists() after the mkdir() call (or even mkdirs())

Upvotes: 0

Buhake Sindi
Buhake Sindi

Reputation: 89169

The answer is simple, you're trying to create nested folders (a folder inside a folder). For nested folders use File.mkdirs(). That works, (tested).

Upvotes: 3

koregan
koregan

Reputation: 10164

If you use something like process monitor for windows you can view the OS level attempt to create the directory.

That may give you the info you need.

You'll probably need to make use of the filters in process monitor because there's usually a lot of disk activity going on :)

Upvotes: 1

Holly Cummins
Holly Cummins

Reputation: 11482

You will need to use mkdirs() if the parent folder (some in your example) doesn't already exist.

Upvotes: 9

Jonathan W
Jonathan W

Reputation: 3799

I don't think you can, at least not from Java. Being that the OS makes that determination, Java is simply delegating to it and returning the result.

Have you tried ensuring that your File object is pointing where you think it is?

Update: if C:/some does not exist, it must first be created before you can attempt to create C:/some/path. Or use mkdirs() as was pointed out.

Upvotes: 1

Related Questions