Java Delete Tree deltree(String directory)

Here you can find the source of deltree(String directory)

Description

Recursive delete of a directory and all it's content

License

Open Source License

Parameter

Parameter Description
directory a parameter

Declaration

public static void deltree(String directory) 

Method Source Code

//package com.java2s;

import java.io.File;

import java.util.ArrayList;

import java.util.List;

public class Main {
    /**/*from  www .ja v  a  2 s .co m*/
     * Recursive delete of a directory and all it's content
     * 
     * @param directory
     */
    public static void deltree(String directory) {
        deltree(new File(directory));
    }

    /**
     * Recursive delete of a directory and all it's content
     * 
     * @param directory
     */
    public static void deltree(File directory) {
        if (directory.exists() && directory.isDirectory()) {
            File[] fileArray = directory.listFiles();
            if (fileArray != null) {
                for (int i = 0; i < fileArray.length; i++) {
                    if (fileArray[i].isDirectory()) {
                        deltree(fileArray[i]);
                    } else {
                        fileArray[i].delete();
                    }
                }
            }

            directory.delete();
        }
    }

    public static boolean exists(String fileName) {
        File file = new File(fileName);

        return file.exists();
    }

    public static String[] listFiles(String fileName) {
        return listFiles(new File(fileName));
    }

    public static String[] listFiles(File file) {
        List<String> files = new ArrayList<String>();

        File[] fileArray = file.listFiles();
        if (fileArray != null) {
            for (int i = 0; i < fileArray.length; i++) {
                if (fileArray[i].isFile()) {
                    files.add(fileArray[i].getName());
                }
            }
        }

        return (String[]) files.toArray(new String[0]);
    }
}

Related

  1. delTree(File root)
  2. deltree(File root)
  3. deltree(final String directory)
  4. deltree(String dir)
  5. deltree(String dir)
  6. delTree(String dirName, String excludedDirName)