java 删除文件和目录
public void deleteFileAndDir(String filePathDir) { Path path = Paths.get(filePathDir); try { Files.walkFileTree(path, new SimpleFileVisitor<Path>() { // 先去遍历删除文件 @Override public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException { Files.delete(file); System.out.printf("文件被删除 : %s%n", file); return FileVisitResult.CONTINUE; } // 再去遍历删除目录 @Override public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException { Files.delete(dir); System.out.printf("文件夹被删除: %s%n", dir); return FileVisitResult.CONTINUE; } } ); } catch (Exception e) { log.error("删除文件和文件夹失败,路径={}", filePathDir); } }
随笔看心情