File.mkdir() and mkdirs() are creating file instead of directory

artem picture artem · Dec 19, 2012 · Viewed 41k times · Source

I use the following code:

final File newFile = new File("/mnt/sdcard/test/");
newFile.mkdir(); // if I use mkdirs() result is the same

And it creates an empty file! Why?

Answer

colin-higgins picture colin-higgins · Dec 19, 2012

You wouldn't use mkdirs() unless you wanted each of those folders in the structure to be created. Try not adding the extra slash on the end of your string and see if that works.

For example

final File newFile = new File("/mnt/sdcard/test");
newFile.mkdir();