Java Utililty Methods Path Delete nio

List of utility methods to do Path Delete nio

Description

The list of methods to do Path Delete nio are organized into topic(s).

Method

voiddeleteRecursively(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);
...
voiddeleteRecursively(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>() {
...
voiddeleteTmpDir(Path path)
delete Tmp Dir
try {
    Files.deleteIfExists(tmpBase);
} catch (Exception e) {
    e.printStackTrace();
voiddeleteTreeBelowPath(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) {
...
voiddoDelete(@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);
voidforceDelete(Path path)
Forces the deletion of the file or directory corresponding to the specified path.
Files.walkFileTree(path, DELETER);
voidoptimisticDelete(Path path)
Delete a file (not recursively) and ignore any errors.
if (path == null) {
    return;
try {
    Files.delete(path);
} catch (IOException ignored) {
StringreadAndDelete(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();
voidrecursiveDelete(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 {
...
voidrecursiveDelete(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
...