4

我有一个包含很多文件的目录。我想删除整个目录以及其中的所有文件。

我希望我的代码等到该目录中的每个文件(包括目录本身)都被删除,然后再执行下一个命令。

我要怎么等?我的代码是

public void wipeMemoryCard() 
    {
        File deleteMatchingFile = new File(Environment 
                .getExternalStorageDirectory().toString()); 
        try { 
            filenames = deleteMatchingFile.listFiles(); 
            if (filenames != null && filenames.length > 0) 
            { 
                content = true;
                for (File tempFile : filenames) 
                { 
                    if (tempFile.isDirectory()) 
                    { 
                        wipeDirectory(tempFile.toString()); 
                        tempFile.delete();

                    } 
                    else 
                    {                       
                        File file = new File(tempFile.getAbsolutePath()); 
                        file.delete(); 
                    } 
                } 
            } 
            else 
            {   

                deleteMatchingFile.delete(); 
                Toast("No files to Delete");
            } 
        } 

        catch (Exception e) 
        { 
           e.printStackTrace();
        }
        if(content == true)
        {
              if (filenames == null && filenames.length == 0) 
              {
                  Toast("Files Deleted");
              }
        }
    } 

    private static void wipeDirectory(String name) { 
        File directoryFile = new File(name); 
        File[] filenames = directoryFile.listFiles(); 
        if (filenames != null && filenames.length > 0) 
        { 
            for (File tempFile : filenames) 
            { 
                if (tempFile.isDirectory()) 
                { 
                    wipeDirectory(tempFile.toString()); 
                    tempFile.delete(); 
                }
                else 
                { 
                    File file = new File(tempFile.getAbsolutePath()); 
                    file.delete();  
                } 
            } 
        } else 
        { 
            directoryFile.delete(); 
        } 
    } 
4

3 回答 3

12

You should not run this on the UI thread. If the file deletion takes too long, the system will pop up an "Application Not Responding" error. You can do this with an AsyncTask. The documentation shows a simple way to use this to pop up a "please wait" dialog, do the time-consuming work in the background, and then dismiss the dialog.

P.S. Your method name is kind of scary! :)

于 2012-01-10T05:59:20.530 回答
1

您应该为此使用处理程序,因此当所有文件被删除时,它将向处理程序发送消息到您要执行的下一个任务。

请参阅此链接以获取处理程序..

http://www.tutorialforandroid.com/2009/01/using-handler-in-android.html

希望你问这个......

于 2012-01-10T05:57:30.133 回答
0
public static void DeleteRecursive(String filename) {
    File file = new File(filename);
    if (!file.exists())
        return;
    if (!file.isDirectory()) {
        file.delete();
        return;
    }

    String[] files = file.list();
    for (int i = 0; i < files.length; i++) {

        DeleteRecursive(filename + "/" + files[i]);
    }
    file.delete();
}
于 2012-01-10T06:41:08.890 回答