Java File Path Delete deleteAllFiles(File dirOrFile)

Here you can find the source of deleteAllFiles(File dirOrFile)

Description

Recursively delete all the files and folders under dirOrFile

License

Open Source License

Parameter

Parameter Description
dirOrFile - the File object which could be either a folder or a file.

Declaration

public static void deleteAllFiles(File dirOrFile) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2004,2009 Actuate Corporation.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:/*w  w w  . j  a  v a 2 s  .  c om*/
 *  Actuate Corporation  - initial API and implementation
 *******************************************************************************/

import java.io.File;

public class Main {
    /**
     * Recursively delete all the files and folders under dirOrFile
     * 
     * @param dirOrFile -
     *            the File object which could be either a folder or a file.
     */
    public static void deleteAllFiles(File dirOrFile) {
        if (!dirOrFile.exists())
            return;

        if (dirOrFile.isFile()) {
            dirOrFile.delete();
        } else
        // dirOrFile is directory
        {
            if ((dirOrFile.listFiles() != null) && (dirOrFile.listFiles().length > 0)) {
                File[] fileList = dirOrFile.listFiles();
                for (int i = 0; i < fileList.length; i++)
                    deleteAllFiles(fileList[i]);
            }

            // Directory can only be deleted when it is empty.
            dirOrFile.delete();
        }
    }
}

Related

  1. deleteAllFile(String path)
  2. deleteAllFiles()
  3. deleteAllFiles(File dir)
  4. deleteAllFiles(File directory)
  5. deleteAllFiles(File directory)
  6. deleteAllFiles(String directory)
  7. deleteAllFiles(String folder, String type)
  8. deleteAllFiles(Vector filesToDelete)
  9. deleteAllFilesAux(String input, File swapFolder)