在我的应用中,我使用内部存储即文件保存了所有数据。所以在第一个实例中使用ContextWrapper cw = new ContextWrapper(getApplicationContext());
类我得到的目录路径为m_AllPageDirectoryPath = cw.getDir("AllPageFolder", Context.MODE_PRIVATE);
在这个目录路径中,我将一些文件File保存为Page01,page02,Page03等等。
再次在Page01中我保存了一些文件,如image01,image02 ...使用相同的概念m_PageDirectoryPath = cw.getDir("Page01", Context.MODE_PRIVATE);
现在删除m_AllPageDirectoryPath我想删除与之关联的所有文件。我尝试使用此代码,但它不起作用。
File file = new File(m_AllPageDirectoryPath.getPath());
file.delete();
答案 0 :(得分:2)
只有目录为空时,您的代码才有效。
如果您的目录包含文件和子目录,则必须递归删除所有文件 ..
试试这段代码,
// Deletes all files and subdirectories under dir.
// Returns true if all deletions were successful.
// If a deletion fails, the method stops attempting to delete and returns false.
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();
}
(实际上你必须在问这个问题之前在互联网上搜索)