Java Recursive Delete recursiveDelete(File f)

Here you can find the source of recursiveDelete(File f)

Description

Recursively delete files starting with the one that is given.

License

Open Source License

Parameter

Parameter Description
f File to delete.

Declaration

public static synchronized void recursiveDelete(File f) 

Method Source Code

//package com.java2s;
/*//from w w  w .ja  v  a 2  s.  c  om
 * FileUtils.java 
 *
 * Created by OneBigCD
 * Copyright (c) 2004-2013, Pat Farrell, All rights reserved.
 * 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. * 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 {
    /**
     * Recursively delete files starting with the one that is given.
     * Attempt to do this "Atomically" by moving the file/dir
     * to a new location first then deleting from there.
     * 
     * @param f File to delete.
     */
    public static synchronized void recursiveDelete(File f) {
        if (f == null)
            return;
        if (!f.exists())
            return;

        // find a parent dir to move to before delete
        File parent = f.getParentFile();
        if (parent == null) {
            throw new IllegalArgumentException("Cannot delete a root directory");
        }
        // delete "atomically" by renaming first
        // then deleting all of the parts.
        File moveTo = new File(parent, "rdelete" + System.currentTimeMillis());
        boolean moved = f.renameTo(moveTo);
        if (moved == true)
            f = moveTo;
        simpleRecursiveDelete(f);
    }

    /** delete speficied file and all subdirectories
     * @param f file to delete
     */
    private static synchronized void simpleRecursiveDelete(File f) {
        if (!f.isDirectory()) {
            f.delete();
            return;
        }
        String[] files = f.list();
        if (files == null)
            return;
        for (int i = 0; i < files.length; i++) {
            File theFile = new File(f, files[i]);
            simpleRecursiveDelete(theFile);
        }
        f.delete();
    }
}

Related

  1. recursiveDelete(File dir)
  2. recursiveDelete(File dir, boolean deleteBase)
  3. recursiveDelete(File dirPath)
  4. recursiveDelete(File f)
  5. recursiveDelete(File f)
  6. recursiveDelete(File f)
  7. recursiveDelete(File file)
  8. recursiveDelete(File file)
  9. recursiveDelete(File file)