Java File Delete nio deleteIfEmpty(final File directory)

Here you can find the source of deleteIfEmpty(final File directory)

Description

delete If Empty

License

Open Source License

Declaration

public static final boolean deleteIfEmpty(final File directory) throws IOException 

Method Source Code


//package com.java2s;

import java.io.File;
import java.io.FileFilter;

import java.io.FileNotFoundException;
import java.io.IOException;

import java.nio.file.Files;

public class Main {
    public static final boolean deleteIfEmpty(final File directory) throws IOException {
        if (directory.list().length > 0)
            return false;
        Files.delete(directory.toPath());
        return true;
    }/*from   w w  w.  j  ava2s .c  o  m*/

    public static void delete(final File directory, final FileFilter fileFilter) throws IOException {
        if (!directory.exists() || !directory.isDirectory())
            throw new FileNotFoundException("No directory found at " + directory);
        for (File file : directory.listFiles(fileFilter))
            Files.delete(file.toPath());
    }
}

Related

  1. deleteFile(String fileName)
  2. deleteFile(String filename)
  3. deleteFileCascade(String directory)
  4. deleteFileRescursive(File file)
  5. deleteFiles(File directory, String affix)
  6. deleteLocalFile(final String fileName)
  7. deleteLocalFileOrDirectory(File file)
  8. deleteMatching(File baseFile, String regex)
  9. deletePidFile()