androidjava-io

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


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?


Solution

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