Java Force Delete forceDelete(File file)

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

Description

force Delete

License

Apache License

Declaration

public static void forceDelete(File file) throws IOException 

Method Source Code

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

import java.io.File;
import java.io.FileFilter;

import java.io.FileNotFoundException;

import java.io.IOException;

import java.util.Locale;

public class Main {
    private static final char SYSTEM_SEPARATOR = File.separatorChar;

    public static void forceDelete(File file) throws IOException {
        if (file.isDirectory()) {
            deleteDirectory(file);/*w w w.j  a v a2 s.  c o m*/
        } else {
            boolean filePresent = file.exists();
            if (!file.delete()) {
                if (!filePresent) {
                    throw new FileNotFoundException("File does not exist: "
                            + file);
                }
                String message = "Unable to delete file: " + file;
                throw new IOException(message);
            }
        }
    }

    public static void deleteDirectory(File directory) throws IOException {
        if (!directory.exists()) {
            return;
        }
        if (!isSymlink(directory)) {
            cleanDirectory(directory);
        }
        if (!directory.delete()) {
            String message = "Unable to delete directory " + directory
                    + ".";
            throw new IOException(message);
        }
    }

    public static boolean isSymlink(File file) throws IOException {
        if (file == null) {
            throw new NullPointerException("File must not be null");
        }
        if (isSystemWindows()) {
            return false;
        }
        File fileInCanonicalDir = null;
        if (file.getParent() == null) {
            fileInCanonicalDir = file;
        } else {
            File canonicalDir = file.getParentFile().getCanonicalFile();
            fileInCanonicalDir = new File(canonicalDir, file.getName());
        }
        if (fileInCanonicalDir.getCanonicalFile().equals(
                fileInCanonicalDir.getAbsoluteFile())) {
            return false;
        }
        return true;
    }

    public static void cleanDirectory(File directory) throws IOException {
        if (!directory.exists()) {
            String message = directory + " does not exist";
            throw new IllegalArgumentException(message);
        }
        if (!directory.isDirectory()) {
            String message = directory + " is not a directory";
            throw new IllegalArgumentException(message);
        }
        File[] files = directory.listFiles();
        if (files == null) {
            throw new IOException("Failed to list contents of " + directory);
        }
        IOException exception = null;
        for (File file : files) {
            try {
                forceDelete(file);
            } catch (IOException ioe) {
                exception = ioe;
            }
        }
        if (null != exception) {
            throw exception;
        }
    }

    static boolean isSystemWindows() {
        return SYSTEM_SEPARATOR == '\\';
    }

    public static File[] listFiles(File dir, String suffix) {
        File[] files = null;
        if (dir.exists()) {
            files = dir.listFiles(new FileFilter() {
                public boolean accept(File pathName) {
                    if ((pathName.isFile())
                            && (pathName.getName().toLowerCase(
                                    Locale.ENGLISH).endsWith(suffix))) {
                        return true;
                    }
                    return false;
                }
            });
        }
        if (files == null) {
            return new File[0];
        }
        return files;
    }
}

Related

  1. forceDelete(File f)
  2. forceDelete(File file)
  3. forceDelete(File file)
  4. forceDelete(File file)
  5. forceDelete(File file)
  6. forceDelete(File file)
  7. forceDelete(File file)
  8. forceDelete(File file)
  9. forceDelete(File path)