Java File Delete nio deleteFile(File f)

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

Description

Helper function used to delete a file.

License

Open Source License

Parameter

Parameter Description
f - The file to delete.

Exception

Parameter Description
IOException - If anything during the operation fails.

Declaration

public static void deleteFile(File f) throws IOException 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.io.File;

import java.io.IOException;
import java.nio.file.Files;

public class Main {
    /**/*from w ww  .  j a v a2 s.c o  m*/
     * Helper function used to delete a file. Directories are only deleted if empty.
     *
     * @param f - The file to delete.
     * @throws IOException - If anything during the operation fails.
     */
    public static void deleteFile(File f) throws IOException {
        deleteFile(f, false);
    }

    /**
     * Helper function used to delete a file.
     * If a directory and we want it, recursively delete all sub files and directories.
     *
     * @param f - The file to delete.
     * @param recursive - Whether to delete recursively files in folders. If not, only empty folder will be deleted.
     * @throws IOException - If anything during the operation fails.
     */
    public static void deleteFile(File f, boolean recursive) throws IOException {
        if (recursive && f.isDirectory()) {
            for (File c : f.listFiles()) {
                deleteFile(c, recursive);
            }
        }
        Files.deleteIfExists(f.toPath());
    }
}

Related

  1. delete(File toRecurse)
  2. delete(final File directory, final FileFilter fileFilter)
  3. deleteAll(File f)
  4. deleteAll(File file)
  5. deleteEntriesFromZip(File zipFile, List entriesToDelete)
  6. deleteFile(File f)
  7. deleteFile(String fileName)
  8. deleteFile(String filename)
  9. deleteFileCascade(String directory)