Java Path Delete nio delete(final String[] paths)

Here you can find the source of delete(final String[] paths)

Description

delete

License

LGPL

Declaration

public static void delete(final String[] paths) throws IOException 

Method Source Code


//package com.java2s;
//License from project: LGPL 

import java.io.*;
import java.nio.file.*;
import java.nio.file.attribute.BasicFileAttributes;

public class Main {
    public static void delete(final String[] paths) throws IOException {
        for (final String path : paths) {
            delete(path);// w  ww  .j a  v a 2  s . c o m
        }
    }

    public static void delete(final String path) throws IOException {
        final File file = new File(path);
        if (file.exists()) {
            if (file.isFile()) {
                file.delete();
            } else {
                final Path root = Paths.get(path);
                Files.walkFileTree(root, new SimpleFileVisitor<Path>() {
                    @Override
                    public FileVisitResult visitFile(final Path file, final BasicFileAttributes attrs)
                            throws IOException {
                        Files.delete(file);
                        return FileVisitResult.CONTINUE;
                    }

                    @Override
                    public FileVisitResult preVisitDirectory(final Path file, final BasicFileAttributes attrs)
                            throws IOException {
                        if (!file.toString().equalsIgnoreCase(root.toString())) {
                            delete(file.toString());
                        }
                        return FileVisitResult.CONTINUE;
                    }
                });
                try {
                    file.delete();
                } catch (Throwable ignored) {
                }
            }
        }
    }

    public static boolean exists(final String fileName) {
        return (new File(fileName)).exists();
    }
}

Related

  1. cleanUp(boolean isDeleteOriginalFiles, Path[] filesToZip)
  2. clearAndDeleteDirecotry(Path path)
  3. createDeleteOnExitFile(Path path)
  4. delete(@Nullable Path path)
  5. delete(final Path path)
  6. delete(Path path)
  7. delete(Path path)
  8. delete(Path path)
  9. delete(Path root)