Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

java

file-io

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);
}
like image 587
auser Avatar asked Dec 09 '22 00:12

auser


2 Answers

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

like image 172
Holly Cummins Avatar answered Jan 09 '23 20:01

Holly Cummins


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).

like image 43
Buhake Sindi Avatar answered Jan 09 '23 20:01

Buhake Sindi