JkPathTree implements Closeable { public JkPathTree deleteContent() { if (!Files.exists(getRoot())) { return this; } JkUtilsPath.walkFileTree(getRoot(), new SimpleFileVisitor<Path>() { @Override public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) { return visitFile(file); } @Override public FileVisitResult postVisitDirectory(Path dir, IOException exc) { return visitDir(dir); } private FileVisitResult visitFile(Path path) { if (matcher.matches(getRoot().relativize(path))) { JkUtilsPath.deleteFile(path); } return FileVisitResult.CONTINUE; } private FileVisitResult visitDir(Path path) { if (!JkUtilsPath.isSameFile(getRoot(), path) && matcher.matches(getRoot().relativize(path)) && JkUtilsPath.listDirectChildren(path).isEmpty()) { JkUtilsPath.deleteFile(path); } return FileVisitResult.CONTINUE; } }); return this; } private  JkPathTree(RootHolder rootHolder, JkPathMatcher matcher); static JkPathTree of(Path rootDir); static JkPathTree ofZip(Path zipFile); Path getRoot(); Path getRootDirOrZipFile(); JkPathMatcher getMatcher(); boolean isDefineMatcher(); boolean exists(); JkPathTree createIfNotExist(); Stream<Path> stream(FileVisitOption ...options); List<Path> getRelativeFiles(); List<Path> getFiles(); JkPathTree goTo(String relativePath); JkPathTree resolvedTo(Path newRoot); Path get(String relativePath); JkPathTree importDir(Path dirToCopy, CopyOption... copyOptions); JkPathTree importTree(JkPathTree tree, CopyOption... copyOptions); JkPathTree importFiles(Iterable<Path> files, StandardCopyOption ... copyOptions); JkPathTree importFile(Path src, String targetName, StandardCopyOption ... copyOptions); JkPathTree deleteContent(); JkPathTree deleteRoot(); JkPathTree zipTo(Path destination); int copyTo(Path destinationDir, CopyOption ... copyOptions); void copyFile(String sourcePath, Path destinationDir, CopyOption ... copyOptions); JkPathTree andMatcher(PathMatcher pathMatcher); JkPathTree withMatcher(JkPathMatcher pathMatcher); JkPathTree andMatching(boolean positive, String... globPatterns); JkPathTree andMatching(String... globPatterns); JkPathTree andMatching(boolean positive, Iterable<String> globPatterns); int count(int max, boolean includeDirectories); JkPathTree resolve(Path path); JkPathTreeSet toSet(); @Override String toString(); @Override void close();  }
@Test public void testDeleteContent() throws IOException { Path foo = Files.createTempDirectory("foo"); Path bar = foo.resolve("bar"); Files.createDirectories(bar); Path txt = bar.resolve("file.txt"); Files.write(txt, "toto".getBytes(Charset.forName("UTF8"))); Path txt2 = foo.resolve("file2.txt"); Files.copy(txt, txt2); assertTrue(Files.exists(txt)); JkPathTree fooTree = JkPathTree.of(foo).andMatching(false, "bar/**", "bar"); System.out.println(fooTree.getFiles()); assertFalse(fooTree.getFiles().contains(txt)); fooTree.deleteContent(); assertTrue(Files.exists(txt)); fooTree.deleteRoot(); }