删除文件和目录不起作用

时间:2014-12-24 22:39:23

标签: java android directory delete-file

我试图删除目录中的所有文件,然后使用下面的代码删除目录,但它似乎不起作用。 (方法运行后不删除文件。)

public void DeleteDirectory() {
        ArrayList<File> Directories = new ArrayList<File>();
        Directories.add(new File(Environment.getExternalStorageDirectory().toString().concat("/AssetControl/Images")));
        Directories.add(new File(Environment.getExternalStorageDirectory().toString().concat("/AssetControl/Thumbnails")));

        ListIterator<File> itr = Directories.listIterator();

        while (itr.hasNext()) {
            File dir = itr.next();
                if (dir.isDirectory()) {
                String[] files = dir.list();
                for (int i = 0; i < files.length; i++) {
                    (new File(files[i])).delete();
                    // This is also not working:
                    // File current = new File(files[i]);
                    // current.delete();
                }
                dir.delete();
            }
        }

    }

1 个答案:

答案 0 :(得分:0)

这对我有用: 用File []文件替换了String []文件。

        while (itr.hasNext()) {
            File dir = itr.next();
                if (dir.isDirectory()) {
                File[] files = dir.listFiles();
                for (int i = 0; i < files.length; i++) {
                    DeleteFile(files[i]);
                }
                dir.delete();
            }
        }