Java File Delete clearFileOrDir(File file)

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

Description

Clear directory

License

Open Source License

Parameter

Parameter Description
file to be cleared

Declaration

public static void clearFileOrDir(File file) 

Method Source Code

//package com.java2s;

import java.io.File;

public class Main {
    /**/*www .  j  a  v  a 2 s . co m*/
     * Clear directory
     * 
     * @param file to be cleared
     */
    public static void clearFileOrDir(File file) {
        if (!file.exists()) {
            return;
        }
        if (file.isFile()) {
            file.delete();
            return;
        }
        File[] children = file.listFiles();
        if (children == null || children.length == 0) {
            file.delete();
            return;
        }
        for (File child : children) {
            clearFileOrDir(child);
        }
        file.delete();
    }

    /**
     * Clear directory
     * 
     * @param dir to be cleared
     */
    public static void clearFileOrDir(String dir) {
        File file = new File(dir);
        clearFileOrDir(file);
    }
}

Related

  1. clearFile(File file)
  2. clearFile(String directory)
  3. clearFile(String pFileName)
  4. delete(File aFile)
  5. delete(File classDir)
  6. delete(File current)
  7. delete(File dir)