List<File> filelist = new ArrayList<File>();
public void traverseFolder2(String path) {
File file = new File(path);
if (file.exists()) {
File[] files = file.listFiles();
if (files.length == 0) {
return;
} else {
for (File file2 : files) {
if (file2.isDirectory()) {
traverseFolder2(file2.getAbsolutePath());
} else {
filelist.add(file2);
}
}
}
} else {
}
}
/**
* 删除指定目录下文件及目录
* @param deleteThisPath
* @param filepath
* @return
*/
public void deleteFolderFile(String filePath, boolean deleteThisPath) {
if (!TextUtils.isEmpty(filePath)) {
try {
File file = new File(filePath);
if (file.isDirectory()) {// 处理目录
File files[] = file.listFiles();
for (int i = 0; i < files.length; i++) {
deleteFolderFile(files[i].getAbsolutePath(), true);
}
}
if (deleteThisPath) {
if (!file.isDirectory()) {// 如果是文件,删除
file.delete();
} else {// 目录
if (file.listFiles().length == 0) {// 目录下没有文件或者目录,删除
file.delete();
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}