public static void main(String[] args) {
String folderPath = "D:\\Test\\mp4"; //替换为自己的文件夹即可
try {
deleteFolder(Paths.get(folderPath));
System.out.println("成功删除文件夹");
} catch (IOException e) {
e.printStackTrace();
}
}
public static void deleteFolder(Path folderPath) throws IOException {
if (Files.exists(folderPath)) {
Files.walkFileTree(folderPath, EnumSet.noneOf(FileVisitOption.class), Integer.MAX_VALUE, new SimpleFileVisitor<Path>() {
@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
Files.delete(file);
return FileVisitResult.CONTINUE;
}
@Override
public FileVisitResult visitFileFailed(Path file, IOException exc) throws IOException {
return FileVisitResult.CONTINUE;
}
@Override
public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException {
if (exc == null) {
Files.delete(dir);
return FileVisitResult.CONTINUE;
} else {
return FileVisitResult.CONTINUE;
}
}
});
}
}