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!)
- I checked dest folder existence.
- I try to make dest folder.
- I checked type of dest's parent (folder or not)
- I try to delete file which has same name of dest folder. (if exist)
- 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.