代码
java
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
java
/**
* 删除文件夹及文件夹下所有子目录及文件
*/
public void deleteFolder() {
// 路径
Path directory = Paths.get("/your/delete/path");
try (Stream<Path> stream = Files.walk(directory).sorted(Comparator.reverseOrder())) {
stream.forEach(path -> {
try {
Files.delete(path);
log.info("删除文件->:{}", path);
} catch (IOException e) {
log.error("删除失败->:", e);
}
});
} catch (IOException e) {
log.error("删除失败->:", e);
}
}