-1

I have a problem with File.mkdirs().

[my test code]

public boolean makeFolder(Context context, String path){
    if(path == null || context == null)
       return false;

    File itemPath = new File(path);
    if(itemPath.isDirectory())
        return true; //happy time1 : because already exist.

    if(itemPath.mkdirs())
        return true; //happy time2 : because It's is goal.

    //now we have trouble.
    //itemPath is not directory and can not make that folder.
    //is itemPath one of files? ok find it out.

    // first of all, check a paranet folder path.
    File parent = itempath.getParentFile();
    if(parent == null) 
        return false;

    if(parent.isDirectory()){
        if(itemPath.isFile()){
            if(itemPath.delete() == false){
                return false;
            }
        }else{
            //This is the spot where I stocked.
            sendDownloadErrorLog("failed!!");
            return false;  //check point!!!
        }
    }else{
        return false;// parent is't folder!! How I can handle this!
    }
    //once more try make folder (because we try to delete same name file
    return itemPath.mkdirs();
}

I used this code when I make folder before downloading some files. and this code really really works well but not every time. Rarely I was reported "failed" exactly that spot! (check point!)

  1. I checked dest folder existence.
  2. I try to make dest folder.
  3. I checked type of dest's parent (folder or not)
  4. I try to delete file which has same name of dest folder. (if exist)
  5. Finally, once more try to make a dest folder.

but sometimes step 4 was failed. itemPath(dest folder) is not exist. itemPath cant making up. itemPath's parent exist and she is folder. there is no file which has same name of itemPath. so why can't File.mkdirs make destFolder?

Is there any logical hole?

please let me know.

user207421
  • 305,947
  • 44
  • 307
  • 483

1 Answers1

1
if(itemPath.isDirectory())
    return true; //happy time1 : because already exist.

OK

if(itemPath.mkdirs())
    return true; //happy time2 : because It's is goal.

OK

//now we have trouble.
//itemPath is not directory and can not make that folder.

That could mean several things:

  1. It is a file.
  2. You don't have write access to the parent directory.
  3. The parent isn't a directory.

So:

if(parent.isDirectory()){
    if(itemPath.isFile()){
        if(itemPath.delete() == false){
            return false;

This is the case where item is a file, parent is a directory, and you don't have write access to the file or possibly the directory.

        }
    }else{
        //This is the spot where I stocked.

At this point, the parent is the directory, the item isn't a file, so it doesn't exist, and you failed to create it above, so you probably don't have write access to the parent directory.

        sendDownloadErrorLog("failed!!");
        return false;  //check point!!!
    }
}else{
    return false;// parent is't folder!! How I can handle this!

You can't and you shouldn't handle this. It is a user error, or a configuration problem. Not something to try and code around.

//once more try make folder (because we try to delete same name file
return itemPath.mkdirs();

I would say you are overcomplicating this. If you're supposed to be creating a directory and you can't because a file by that name already exists, that is an error, possibly on the part of the user. Tell him. You shouldn't react by deleting the file, it's none of your business why it's there. Similarly, if you don't have write access, that's the end of it. Nothing you can do. I would reduce the whole thing to this:

return itemPath.mkdirs();
user207421
  • 305,947
  • 44
  • 307
  • 483
  • Thanks for you answer. – PodotreeDev Dec 08 '15 at 02:56
  • That part which you said none of my business is defensive code. 1. We got "mnt/android/data/com.test.mine/parent/dest". 2.User try to delete that folder 3. but failed because of some reason. (ex. android hold files inside dest folder etc). 4.after that kind of failure, sometimes android makes zombie folder.(dest folder turned to it) it is not folder. 5. User try to download same file (same file will be downloaded same folder) 6. but there are zombie 7.so we try to delete something which has same name with dest folder. this is my purpose. – PodotreeDev Dec 08 '15 at 03:10