public static boolean deleteFile(final File file) {
try {
if (null != file) {
final LinkedList<File> dirLinkedList = new LinkedList<>();
if (file.exists()) {
if (file.isFile()) {
file.delete();
} else if (file.isDirectory()) {
dirLinkedList.add(file);
for (int i = 0; i < dirLinkedList.size(); ++i) {
final File child = dirLinkedList.get(i);
final File[] items = child.listFiles();
if (null != items) {
for (final File item : items) {
if (item.isFile()) {
item.delete();
} else if (item.isDirectory()) {
dirLinkedList.add(item);
}
}
}
}
}
}
while (dirLinkedList.size() > 0){
dirLinkedList.removeLast().delete();
}
return !file.exists();
}
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
网友评论