Java Delete File Recursively deleteRecursively(File fileOrDir)

Here you can find the source of deleteRecursively(File fileOrDir)

Description

delete Recursively

License

Open Source License

Declaration

public static void deleteRecursively(File fileOrDir) throws IOException 

Method Source Code


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

import java.io.File;

import java.io.IOException;

public class Main {
    public static void deleteRecursively(File fileOrDir) throws IOException {
        if (fileOrDir.isFile()) {
            deleteFile(fileOrDir);/*w  w  w  . ja  va  2 s.  co  m*/
        } else if (fileOrDir.isDirectory()) {
            String[] names = fileOrDir.list();
            if (names != null) {
                for (String name : names) {
                    deleteRecursively(new File(fileOrDir, name));
                }
            }
            if (!fileOrDir.delete()) {
                throw new IOException("Could not delete dir: " + fileOrDir);
            }
        } else {
            throw new IOException("Neither file nor dir: " + fileOrDir);
        }
    }

    public static void deleteFile(File file) throws IOException {
        if (!file.delete()) {
            throw new IOException("Could not delete file: " + file);
        }
    }
}

Related

  1. deleteRecursively(File file)
  2. deleteRecursively(File file)
  3. deleteRecursively(File file)
  4. deleteRecursively(File fileEntry)
  5. deleteRecursively(File fileOrDir)
  6. deleteRecursively(File fileToDelete)
  7. deleteRecursively(File fRoot)
  8. deleteRecursively(File name)
  9. deleteRecursively(File root)