Java Path Delete nio delete(Path path)

Here you can find the source of delete(Path path)

Description

delete

License

Open Source License

Declaration

public static void delete(Path path) throws IOException 

Method Source Code


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

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

public class Main {
    public static void delete(Path path) throws IOException {
        if (!Files.exists(path)) {
            return;
        }//from  www  .j a  v  a 2 s  . c o m

        if (Files.isRegularFile(path)) {
            Files.delete(path);
        } else {
            Files.walkFileTree(path, new SimpleFileVisitor<Path>() {
                @Override
                public FileVisitResult visitFile(Path path, BasicFileAttributes basicFileAttributes)
                        throws IOException {
                    Files.delete(path);
                    return super.visitFile(path, basicFileAttributes);
                }

                @Override
                public FileVisitResult postVisitDirectory(Path path, IOException e) throws IOException {
                    Files.delete(path);
                    return super.postVisitDirectory(path, e);
                }
            });
        }
    }
}

Related

  1. clearAndDeleteDirecotry(Path path)
  2. createDeleteOnExitFile(Path path)
  3. delete(@Nullable Path path)
  4. delete(final Path path)
  5. delete(final String[] paths)
  6. delete(Path path)
  7. delete(Path path)
  8. delete(Path root)
  9. delete(Path targetPath)