Java Path Delete nio deleteQuietly(@Nullable Path path)

Here you can find the source of deleteQuietly(@Nullable Path path)

Description

delete Quietly

License

Open Source License

Declaration

public static void deleteQuietly(@Nullable Path path) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.io.IOException;
import java.nio.file.*;
import java.nio.file.attribute.BasicFileAttributes;

import static java.nio.file.FileVisitResult.CONTINUE;
import static java.nio.file.Files.*;

public class Main {
    public static void deleteQuietly(@Nullable Path path) {
        if (path == null || !exists(path)) {
            return;
        }//from  w  ww.  ja  va  2 s.com
        if (isRegularFile(path)) {
            try {
                doDelete(path);
            } catch (final IOException ignored) {
            }
            return;
        }
        try {
            walkFileTree(path, new SimpleFileVisitor<Path>() {
                @Override
                public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) {
                    try {
                        doDelete(file);
                    } catch (final IOException ignored) {
                    }
                    return CONTINUE;
                }

                @Override
                public FileVisitResult postVisitDirectory(Path dir, IOException exc) {
                    try {
                        doDelete(dir);
                    } catch (final IOException ignored) {
                    }
                    return CONTINUE;
                }
            });
        } catch (final IOException ignored) {
        }
    }

    private static void doDelete(@Nonnull Path path) throws IOException {
        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);

    }

    public static void delete(@Nullable Path path) throws IOException {
        if (path == null || !exists(path)) {
            return;
        }
        if (isRegularFile(path)) {
            doDelete(path);
            return;
        }
        walkFileTree(path, new SimpleFileVisitor<Path>() {
            @Override
            public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
                doDelete(file);
                return CONTINUE;
            }

            @Override
            public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException {
                doDelete(dir);
                return CONTINUE;
            }
        });
    }
}

Related

  1. deleteLockFile(Path logFile)
  2. deleteNotEmptyDirectory(Path path)
  3. deleteOnExit(Path path)
  4. deletePath(Path path)
  5. deletePathRecursively(String path)
  6. deleteQuietly(Path dir)
  7. deleteRecursive(Path path)
  8. deleteRecursive(Path path)
  9. deleteRecursively(final Path path)