Java File Delete nio deleteFile(File f)

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

Description

delete File

License

Open Source License

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;

public class Main {
    public static void deleteFile(File f) throws IOException {
        if (!f.exists())
            return;
        // Delete the file using java.nio.file.Files.delete() instead of java.io.File.delete()
        // if possible (requires Java 7 or better) so exception can be used for debugging cause of fail
        boolean canUseNio = true;
        try {//from  www.  ja  v a 2  s  . co  m
            Class.forName("java.nio.file.Path");
            // it exists on the classpath
        } catch (ClassNotFoundException e) {
            // it does not exist on the classpath
            canUseNio = false;
        }
        if (canUseNio) {
            java.nio.file.Path fp = f.toPath();
            java.nio.file.Files.delete(fp);
        } else if (!f.delete())
            throw new IOException("Failed to delete file: " + f.getName());
    }
}

Related

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