Android Delete known path of a file or folder

Reprinted: https: //blog.csdn.net/qq_31939617/article/details/89414714
[function]
delete (String delFile): Delete a file or folder

deleteSingleFile (String filePath $ Name): delete a single file

deleteDirectory (String filePath): delete the folder and its contents

[Description]
1, delete method calls deleteSingleFile deleteDirectory methods and methods;

2, deleteDirectory method calls deleteSingleFile method;

3, three methods together, can be used when using the delete method, and a method may be used deleteSingleFile deleteDirectory method alone.

DeleteUtil.class

import android.util.Log;
import android.widget.Toast;

import java.io.File;

public class DeleteUtil {

/ ** delete files, can be a file or folder
* @param delFile you want to delete a folder or file name
* @return deleted successfully return true, otherwise false
* /
public boolean the Delete (String delfile) {
File File = new new File ; (delfile)
(! File.Exists ()) {IF
Toast.makeText (MyApplication.getContext (), "failed to delete file:" + delFile + "does not exist!", Toast.LENGTH_SHORT) .Show ();
return false ;
} the else {
IF (file.isFile ())
return deleteSingleFile (delfile);
the else
return the deleteDirectory (delfile);
}
}

/ ** delete a single file
* @param filePath $ Name the file you want to delete the file name
* @return a single file deleted successfully return true, otherwise false
* /
Private boolean deleteSingleFile (String filePath $ Name) {
File File = new new File ( the Name $ filePath);
// if the file corresponding to the file path exists and is a file, delete
IF (File.Exists () && file.isFile ()) {
IF (File.delete ()) {
the Log. e ( "- Method--", " Copy_Delete.deleteSingleFile: delete a single file" + filePath $ Name + "success!");
return to true;
} the else {
Toast.makeText (MyApplication.getContext (), "delete a single file ! "+ filePath $ Name +" failed ", Toast.LENGTH_SHORT) the .Show ();
return to false;
}
} the else {
Toast.makeText (MyApplication.getContext ()," delete a single file failed: "+ filePath $ Name +" does not exist!",Toast.LENGTH_SHORT).show();
return false;
}
}

/ ** delete files in the directory and the directory
file path * @param filePath you want to delete the directory
* @return directory deleted successfully return true, otherwise false
* /
Private boolean deleteDirectory (String filePath) {
// if not to file dir end of the separator, the file is automatically added delimiter
IF (filePath.endsWith (the File.separator)!)
filePath = + filePath the File.separator;
file = dirFile new new file (filePath);
// if dir corresponding file does not exist, or is not a directory, then exit
IF ((dirFile.exists ()) || (dirFile.isDirectory ())!!) {
Toast.makeText (MyApplication.getContext (), "delete directory failed:" + filePath + "does not exist! ", Toast.LENGTH_SHORT) .Show ();
return false;
}
boolean = Flag to true;
all files // delete folders include subdirectories
file [] = dirFile.listFiles files ();
for (file file: files) {
// delete subfolders
IF (file.isFile ()) {
In Flag = deleteSingleFile (file.getAbsolutePath ());
IF (In Flag!)
BREAK;
}
// delete the subdirectory
the else IF (file.isDirectory ()) {
In Flag = the deleteDirectory (File
.getAbsolutePath ());
IF (! In Flag)
BREAK;
}
}
IF (In Flag) {!
Toast.makeText (MyApplication.getContext (), "delete directory failed!", Toast.LENGTH_SHORT) the .Show ();
return to false;
}
/ / delete the current directory
IF (dirFile.delete ()) {
Log.e ( "- Method--", "Copy_Delete.deleteDirectory: delete the directory" + filePath + "success!");
return to true;
} the else {
Toast. makeText (MyApplication.getContext (), "delete the directory:" + filePath +, Toast.LENGTH_SHORT "failed!").show();
return false;
}
}

}

Guess you like

Origin www.cnblogs.com/la66/p/11948884.html