List of utility methods to do Path Delete nio
void | deleteRecursively(Path directory) Deletes a directory recursively. Files.walkFileTree(directory, new FileVisitor<Path>() { @Override public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException { return FileVisitResult.CONTINUE; @Override public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException { Files.delete(file); ... |
void | deleteRecursively(Path path) delete Recursively try { if (!Files.exists(path, LinkOption.NOFOLLOW_LINKS)) { return; if (!Files.isDirectory(path, LinkOption.NOFOLLOW_LINKS)) { Files.delete(path); } else { Files.walkFileTree(path, new SimpleFileVisitor<Path>() { ... |
void | deleteTmpDir(Path path) delete Tmp Dir try { Files.deleteIfExists(tmpBase); } catch (Exception e) { e.printStackTrace(); |
void | deleteTreeBelowPath(Path startHerePath) Delete the whole file tree below the given path. List<Path> dirPaths = new ArrayList<>(); Files.walk(startHerePath).forEach(path -> { if (Files.isDirectory(path)) { dirPaths.add(path); } else { try { Files.delete(path); } catch (IOException e) { ... |
void | doDelete(@Nonnull Path path) do Delete final FileStore fileStore = Files.getFileStore(path); if (fileStore.supportsFileAttributeView("dos")) { setAttribute(path, "dos:readonly", false); setAttribute(path, "dos:system", false); setAttribute(path, "dos:hidden", false); Files.delete(path); |
void | forceDelete(Path path) Forces the deletion of the file or directory corresponding to the specified path. Files.walkFileTree(path, DELETER); |
void | optimisticDelete(Path path) Delete a file (not recursively) and ignore any errors. if (path == null) { return; try { Files.delete(path); } catch (IOException ignored) { |
String | readAndDelete(final Path p) Read and delete file and return the content. final StringBuilder sb = new StringBuilder(); try (BufferedReader bfr = Files.newBufferedReader(p, Charset.forName("UTF-8"))) { String line = bfr.readLine(); while (line != null) { sb.append(line); line = bfr.readLine(); Files.delete(p); return sb.toString(); |
void | recursiveDelete(Path directory) recursive Delete Files.walkFileTree(directory, new SimpleFileVisitor<Path>() { @Override public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException { Files.delete(file); return FileVisitResult.CONTINUE; @Override public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException { ... |
void | recursiveDelete(Path path) Note: Keep an eye out for recursive delete being added to the core Java API in the future - there are certain circumstances in which this can be unsafe. if (Files.isDirectory(path)) { Files.walkFileTree(path, new SimpleFileVisitor<Path>() { @Override public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException { Files.delete(file); return FileVisitResult.CONTINUE; @Override ... |