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?
mkdir: cannot create directory – Permission denied The reason for this error is that the user you're running the mkdir as, doesn't have permissions to create new directory in the location you specified. You should use ls command on the higher level directory to confirm permissions.
mkdirs() will create the specified directory path in its entirety where mkdir() will only create the bottom most directory, failing if it can't find the parent directory of the directory it is trying to create. In other words mkdir() is like mkdir and mkdirs() is like mkdir -p . new File("/tmp/one/two/three").
In Java, the mkdir() function is used to create a new directory. This method takes the abstract pathname as a parameter and is defined in the Java File class. mkdir() returns true if the directory is created successfully; else, it returns false.
Use: File file = new File("Z:\\results\\results. txt"); You need to double the backslashes in Windows because the backslash character itself is an escape in Java literal strings.
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();
When I need to ensure that all dirs for a file exist, but I have only filepath - i do
new File(FileName.substring(0,FileName.lastIndexOf("/"))).mkdirs();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With