Java Directory Clear clearDirectory(File dir, boolean doTree)

Here you can find the source of clearDirectory(File dir, boolean doTree)

Description

Delete every file within a directory.

License

Open Source License

Parameter

Parameter Description
dir A directory, which exists
doTree If true, then we will clear all subdirectories

Declaration

public static void clearDirectory(File dir, boolean doTree) 

Method Source Code


//package com.java2s;
import java.io.File;

public class Main {
    /**/*w  ww.  j  a  v  a 2s .  co m*/
    Delete every file within a directory.
    If there are any problems, the routine silently returns.
        
    @param dir A directory, which exists
    @param doTree If true, then we will clear all subdirectories
    */
    public static void clearDirectory(File dir, boolean doTree) {
        // get a list of the files
        String[] fileList = dir.list();
        if (fileList == null) { // this isn't a directory
            return;
        }

        for (int ii = 0; ii < fileList.length; ii++) {
            File subFile = new File(dir, fileList[ii]);
            if (subFile.isDirectory()) {
                if (doTree) {
                    clearDirectory(subFile, true);
                    subFile.delete();
                }
            } else {
                subFile.delete();
            }
        }

        return;
    }
}

Related

  1. cleanDirectory(String dir)
  2. cleanDirectory(String dirPath)
  3. clearDirctory(File dir)
  4. clearDirectory(File dir)
  5. clearDirectory(File dir)
  6. clearDirectory(File f)
  7. clearDirectory(File f)
  8. clearDirectory(File src)
  9. clearDirectory(String absolutePath)