Java File Delete nio delete(File file)

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

Description

Recursively delete a file or directory.

License

Apache License

Exception

Parameter Description
IOException if the file or directory couldn't be deleted. Unlike File.delete,which just returns false.

Declaration

public static void delete(File file) throws IOException 

Method Source Code


//package com.java2s;
/*/*from w w w  .jav  a2 s  .  c  om*/
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You 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.*;

import java.nio.file.Files;

public class Main {
    /**
     * Recursively delete a file or directory.
     * 
     * @throws IOException
     *           if the file or directory couldn't be deleted. Unlike File.delete,
     *           which just returns false.
     */
    public static void delete(File file) throws IOException {
        if (!file.exists())
            return;

        if (file.isDirectory()) {
            for (File child : listFiles(file)) {
                delete(child);
            }
        }

        Files.delete(file.toPath());
    }

    /**
     * Recursively delete a file or directory.
     * A description of any files or directories
     * that can not be deleted will be added to failures
     * if failures is non-null.
     * This method tries to delete as much as possible.
     */
    public static void delete(File file, StringBuilder failures) {
        if (!file.exists())
            return;

        if (file.isDirectory()) {
            for (File child : listFiles(file)) {
                delete(child, failures);
            }
        }

        try {
            Files.delete(file.toPath());
        } catch (IOException e) {
            if (failures != null) {
                failures.append("Could not delete ").append(file).append(" due to ").append(e.getMessage())
                        .append('\n');
            }
        }
    }

    /**
     * Basically just like {@link File#listFiles()} but instead of returning null
     * returns an empty array. This fixes bug 43729
     */
    public static File[] listFiles(File dir) {
        File[] result = dir.listFiles();
        if (result == null) {
            result = new File[0];
        }
        return result;
    }

    /**
     * Basically just like {@link File#listFiles(FilenameFilter)} but instead of returning null
     * returns an empty array. This fixes bug 43729
     */
    public static File[] listFiles(File dir, FilenameFilter filter) {
        File[] result = dir.listFiles(filter);
        if (result == null) {
            result = new File[0];
        }
        return result;
    }
}

Related

  1. delete(File file)
  2. delete(File file)
  3. delete(File file)
  4. delete(File file)
  5. delete(File file)
  6. delete(File file)
  7. delete(File toRecurse)
  8. delete(final File directory, final FileFilter fileFilter)
  9. deleteAll(File f)