Java Delete File Recursively deleteRecursive(File file)

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

Description

Recursively deletes the provided file, being careful to not follow symbolic links (but there are still unavoidable race conditions).

License

Open Source License

Declaration

public static void deleteRecursive(File file) throws IOException 

Method Source Code

//package com.java2s;
/*/*from w  w w.  j av a2s.  com*/
 * ao-lang - Minimal Java library with no external dependencies shared by many other projects.
 * Copyright (C) 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018, 2019  AO Industries, Inc.
 *     support@aoindustries.com
 *     7262 Bull Pen Cir
 *     Mobile, AL 36695
 *
 * This file is part of ao-lang.
 *
 * ao-lang is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * ao-lang 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
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with ao-lang.  If not, see <http://www.gnu.org/licenses/>.
 */

import java.io.File;

import java.io.IOException;

public class Main {
    /**
     * Recursively deletes the provided file, being careful to not follow symbolic links (but there are still unavoidable race conditions).
     */
    public static void deleteRecursive(File file) throws IOException {
        if (file.isDirectory()
                // Don't recursively travel across symbolic links (still a race condition here, though)
                && file.getCanonicalPath().equals(file.getAbsolutePath())) {
            File afile[] = file.listFiles();
            if (afile != null)
                for (File f : afile)
                    deleteRecursive(f);
        }
        delete(file);
    }

    /**
     * Deletes the provided file, throwing IOException if unsuccessful.
     */
    public static void delete(File file) throws IOException {
        if (!file.delete())
            throw new IOException("Unable to delete: " + file);
    }
}

Related

  1. deleteRecursive(File file)
  2. deleteRecursive(File file)
  3. deleteRecursive(File file)
  4. deleteRecursive(File file)
  5. deleteRecursive(File file)
  6. deleteRecursive(File file)
  7. deleteRecursive(File file)
  8. deleteRecursive(File root)
  9. deleteRecursive(File src)