Java Delete Directory deleteFiles(File parent, boolean incParent)

Here you can find the source of deleteFiles(File parent, boolean incParent)

Description

deletes files recursively.

License

Apache License

Parameter

Parameter Description
parent file to delete. If this is a directory then any children are deleted.
incParent boolean that determines if the parent file is also deleted.

Exception

Parameter Description

Declaration

public static void deleteFiles(File parent, boolean incParent) throws FileNotFoundException 

Method Source Code


//package com.java2s;
/*/*from w  w  w  . j a v a2  s  .  co  m*/
 * Copyright 2004 - 2012 Cardiff University.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import java.io.*;

public class Main {
    /**
     * deletes files recursively. can optionally delete the parent file as well. So, if the parent file
     * is not a directory, and incParent is false, then nothing will be deleted.
     *
     * @param parent    file to delete. If this is a directory then any children are deleted.
     * @param incParent boolean that determines if the parent file is also deleted.
     * @throws java.io.FileNotFoundException
     */
    public static void deleteFiles(File parent, boolean incParent) throws FileNotFoundException {
        if (!parent.exists()) {
            throw new FileNotFoundException("File does not exist.");
        }
        if (parent.isDirectory() && !(parent.listFiles() == null)) {
            File[] files = parent.listFiles();
            for (int i = 0; i < files.length; i++) {
                deleteFiles(files[i], true);
            }
        }
        if (incParent) {
            parent.delete();
        }
    }
}

Related

  1. deleteFiles(File docFolder)
  2. deleteFiles(File f, boolean del_self)
  3. deleteFiles(File file)
  4. deleteFiles(File folder)
  5. deleteFiles(File folder)
  6. deleteFiles(File[] files)
  7. deleteFiles(File[] files)
  8. deleteFiles(File[] intermediateFiles)
  9. deleteFiles(FileFilter filter)