Java File Delete nio delete(File file)

Here you can find the source of delete(File file)

Description

delete

License

Apache License

Declaration

public static void delete(File file) throws IOException 

Method Source Code


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

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;

public class Main {
    public static void delete(File file) throws IOException {
        if (file.isDirectory()) {
            deleteDirectory(file);//from  www  .j av a  2s.  c  om
            return;
        }
        Files.delete(Paths.get(file.getPath()));
    }

    public static void deleteDirectory(File directory) throws IOException {
        cleanDirectory(directory);
        Files.delete(Paths.get(directory.getAbsolutePath()));
    }

    public static void cleanDirectory(File directory) throws IOException {
        if (!directory.isDirectory()) {
            throw new IllegalArgumentException(directory + " is not a directory");
        }

        File[] files = directory.listFiles();
        if (files == null) {
            throw new IOException("Failed to list contents of " + directory);
        }

        for (File file : files) {
            delete(file);
        }
    }
}

Related

  1. delete(File file)
  2. delete(File file)
  3. delete(File file)
  4. delete(File file)
  5. delete(File file)
  6. delete(File toRecurse)
  7. delete(final File directory, final FileFilter fileFilter)
  8. deleteAll(File f)
  9. deleteAll(File file)