如何删除整个文件夹和内容?
我希望我的应用程序的用户能够删除DCIM文件夹(位于SD卡上并包含子文件夹)。
这是可能的,如果是这样的话?
让我告诉你,首先你不能删除DCIM文件夹,因为它是一个系统文件夹。 当您在手机上手动删除它时,它将删除该文件夹的内容,而不是DCIM文件夹。 您可以使用下面的方法删除它的内容:
根据评论更新
File dir = new File(Environment.getExternalStorageDirectory()+"Dir_name_here"); if (dir.isDirectory()) { String[] children = dir.list(); for (int i = 0; i < children.length; i++) { new File(dir, children[i]).delete(); } }
您可以像这样recursion地删除文件和文件夹:
void deleteRecursive(File fileOrDirectory) { if (fileOrDirectory.isDirectory()) for (File child : fileOrDirectory.listFiles()) deleteRecursive(child); fileOrDirectory.delete(); }
我们可以使用命令行来删除整个文件夹及其内容。
public static void deleteFiles(String path) { File file = new File(path); if (file.exists()) { String deleteCmd = "rm -r " + path; Runtime runtime = Runtime.getRuntime(); try { runtime.exec(deleteCmd); } catch (IOException e) { } } }
示例用途,
deleteFiles("/sdcard/uploads/");
对于只包含文件的文件夹而言,您的方法还不错,但是如果您正在查找也包含子文件夹的scheme,则需要recursion
你也应该捕获返回的返回值,以确保你被允许删除文件
并包括
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
在你的清单
void DeleteRecursive(File dir) { Log.d("DeleteRecursive", "DELETEPREVIOUS TOP" + dir.getPath()); if (dir.isDirectory()) { String[] children = dir.list(); for (int i = 0; i < children.length; i++) { File temp = new File(dir, children[i]); if (temp.isDirectory()) { Log.d("DeleteRecursive", "Recursive Call" + temp.getPath()); DeleteRecursive(temp); } else { Log.d("DeleteRecursive", "Delete File" + temp.getPath()); boolean b = temp.delete(); if (b == false) { Log.d("DeleteRecursive", "DELETE FAIL"); } } } } dir.delete(); }
使用下面的方法删除包含文件和它的子目录的整个主目录。 再次调用这个方法后,调用主目录的delete()目录。
// For to Delete the directory inside list of files and inner Directory public static boolean deleteDir(File dir) { if (dir.isDirectory()) { String[] children = dir.list(); for (int i=0; i<children.length; i++) { boolean success = deleteDir(new File(dir, children[i])); if (!success) { return false; } } } // The directory is now empty so delete it return dir.delete(); }
如果你不需要recursion删除东西,你可以尝试这样的东西:
File file = new File(context.getExternalFilesDir(null), ""); if (file != null && file.isDirectory()) { File[] files = file.listFiles(); if(files != null) { for(File f : files) { f.delete(); } } }
public static void deleteDirectory( File dir ) { if ( dir.isDirectory() ) { String [] children = dir.list(); for ( int i = 0 ; i < children.length ; i ++ ) { File child = new File( dir , children[i] ); if(child.isDirectory()){ deleteDirectory( child ); child.delete(); }else{ child.delete(); } } dir.delete(); } }
有很多答案,但我决定添加自己的,因为它有点不同。 它基于OOP;)
我创build了DirectoryCleaner类,每当我需要清理某个目录时,它都会帮助我。
public class DirectoryCleaner { private final File mFile; public DirectoryCleaner(File file) { mFile = file; } public void clean() { if (null == mFile || !mFile.exists() || !mFile.isDirectory()) return; for (File file : mFile.listFiles()) { delete(file); } } private void delete(File file) { if (file.isDirectory()) { for (File child : file.listFiles()) { delete(child); } } file.delete(); } }
它可以用来解决这个问题的方式:
File dir = new File(Environment.getExternalStorageDirectory(), "your_directory_name"); new DirectoryCleaner(dir).clean(); dir.delete();
请参阅android.os.FileUtils,它隐藏在API 21上
public static boolean deleteContents(File dir) { File[] files = dir.listFiles(); boolean success = true; if (files != null) { for (File file : files) { if (file.isDirectory()) { success &= deleteContents(file); } if (!file.delete()) { Log.w("Failed to delete " + file); success = false; } } } return success; }
来源: https : //android.googlesource.com/platform/frameworks/base/+/master/core/java/android/os/FileUtils.java#414
这是我做的…(简洁和testing)
... deleteDir(new File(dir_to_be_deleted)); ... // delete directory and contents void deleteDir(File file) { if (file.isDirectory()) for (String child : file.list()) deleteDir(new File(file, child)); file.delete(); // delete child file or empty directory }
private static void deleteRecursive(File dir) { //Log.d("DeleteRecursive", "DELETEPREVIOUS TOP" + dir.getPath()); if (dir.isDirectory()) { String[] children = dir.list(); for (int i = 0; i < children.length; i++) { File temp = new File(dir, children[i]); deleteRecursive(temp); } } if (dir.delete() == false) { Log.d("DeleteRecursive", "DELETE FAIL"); } }
从目录中删除所有文件的简单方法:
这是通用的function,通过只调用来删除目录中的所有图像
deleteAllImageFile(上下文);
public static void deleteAllFile(Context context) { File directory = context.getExternalFilesDir(null); if (directory.isDirectory()) { for (String fileName: file.list()) { new File(file,fileName).delete(); } } }
这是一个非recursion的实现,只是为了好玩:
/** * Deletes the given folder and all its files / subfolders. * Is not implemented in a recursive way. The "Recursively" in the name stems from the filesystem command * @param root The folder to delete recursively */ public static void deleteRecursively(final File root) { LinkedList<File> deletionQueue = new LinkedList<>(); deletionQueue.add(root); while(!deletionQueue.isEmpty()) { final File toDelete = deletionQueue.removeFirst(); final File[] children = toDelete.listFiles(); if(children == null || children.length == 0) { // This is either a file or an empty directory -> deletion possible toDelete.delete(); } else { // Add the children before the folder because they have to be deleted first deletionQueue.addAll(Arrays.asList(children)); // Add the folder again because we can't delete it yet. deletionQueue.addLast(toDelete); } } }
我已经把这一个,虽然它的步伐删除任何目录结构的文件夹。
public int removeDirectory(final File folder) { if(folder.isDirectory() == true) { File[] folderContents = folder.listFiles(); int deletedFiles = 0; if(folderContents.length == 0) { if(folder.delete()) { deletedFiles++; return deletedFiles; } } else if(folderContents.length > 0) { do { File lastFolder = folder; File[] lastFolderContents = lastFolder.listFiles(); //This while loop finds the deepest path that does not contain any other folders do { for(File file : lastFolderContents) { if(file.isDirectory()) { lastFolder = file; lastFolderContents = file.listFiles(); break; } else { if(file.delete()) { deletedFiles++; } else { break; } }//End if(file.isDirectory()) }//End for(File file : folderContents) } while(lastFolder.delete() == false); deletedFiles++; if(folder.exists() == false) {return deletedFiles;} } while(folder.exists()); } } else { return -1; } return 0; }
希望这可以帮助。
最安全的代码我知道:
private boolean recursiveRemove(File file) { if(file == null || !file.exists()) { return false; } if(file.isDirectory()) { File[] list = file.listFiles(); if(list != null) { for(File item : list) { recursiveRemove(item); } } } if(file.exists()) { file.delete(); } return !file.exists(); }
检查文件是否存在,处理空值,检查目录是否被删除