Java File Path Delete deleteRecursively(File path, File parentCanonical)

Here you can find the source of deleteRecursively(File path, File parentCanonical)

Description

Deletes a file or sub-directory recursively, but does not follow links within the directory structure.

License

Open Source License

Parameter

Parameter Description
path points to the item being deleted
parentCanonical File#getCanonicalFile() canonical form of the <code>path</code>'s parent

Exception

Parameter Description
IOException if there is an error deleting an item

Declaration


protected static void deleteRecursively(File path, File parentCanonical) throws IOException 

Method Source Code

//package com.java2s;
/**//  www  .  j a  v  a  2  s .  co  m
 *  Copyright 2010-2013 Konstantin Livitski
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the Data-bag Project License.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  Data-bag Project License for more details.
 *
 *  You should find a copy of the Data-bag Project License in the
 *  `data-bag.md` file in the `LICENSE` directory
 *  of this package or repository.  If not, see
 *  <http://www.livitski.name/projects/data-bag/license>. If you have any
 *  questions or concerns, contact the project's maintainers at
 *  <http://www.livitski.name/contact>. 
 */

import java.io.File;

import java.io.IOException;

public class Main {
    /**
     * Deletes a file or sub-directory recursively, but does not follow
     * links within the directory structure.
     * @param path points to the item being deleted
     * @param parentCanonical {@link File#getCanonicalFile() canonical}
     * form of the <code>path</code>'s parent
     * @throws IOException if there is an error deleting an item
     */
    // TODO: unused method - use with caution
    protected static void deleteRecursively(File path, File parentCanonical) throws IOException {
        if (!path.exists())
            return;
        else if (!path.isDirectory()) {
            if (!path.delete())
                throw new IOException("Could not delete file " + path);
        } else {
            File canonical = path.getCanonicalFile();
            File realParent = canonical.getParentFile();
            // do not delete contents of linked directories
            if (path.getName().equals(canonical.getName()) && (null == parentCanonical && null == realParent
                    || null != parentCanonical && parentCanonical.equals(realParent))) {
                for (File child : path.listFiles())
                    deleteRecursively(child, canonical);
            }
            if (!path.delete())
                throw new IOException("Could not delete directory or link " + path);
        }
    }
}

Related

  1. deleteRecursive(File path)
  2. deleteRecursive(File path)
  3. deleteRecursive(File path)
  4. deleteRecursive(File path, boolean debug)
  5. deleteRecursively(File path)
  6. deleteSelectedFilesFromDirectory(final String directoryPath, final List filesList)
  7. deleteSubfiles(String publishTemppath)
  8. deleteTemp(String tempPath)
  9. deleteTempDataFile(String dataFilePath)