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); } }
标签:Files,java,删除,public,FileVisitResult,file,filePathDir,目录 From: https://www.cnblogs.com/stromgao/p/17333239.html