Here you can find the source of deleteFiles(final File file)
Parameter | Description |
---|---|
file | the directory to delete the files from. |
public static boolean deleteFiles(final File file)
//package com.java2s; import java.io.File; import java.io.FileFilter; import java.io.FilenameFilter; import java.io.IOException; import javax.annotation.Nonnull; public class Main { /**//from w w w . j a v a 2 s. c om * Utility function for deleting the files in a given directory and the directory itself. * (Files only, non-recursive). * @param file the directory to delete the files from. * @return true if all the files were successfully deleted. */ public static boolean deleteFiles(final File file) { boolean ok = true; if (file != null) { if (file.isDirectory()) { final File[] files = file.listFiles(); if (files != null) { for (final File f : files) { if (f.isFile()) { ok &= f.delete(); } } } else { ok = false; } ok &= file.delete(); } } return ok; } /** * Wraps {@link File#listFiles()} so it will throw an IOException instead of returning null * @param f directory to list * @return see {@link File#listFiles()}, except this method can never return null * @throws IOException if {@link File#listFiles()} returns null */ @Nonnull public static File[] listFiles(File f) throws IOException { final File[] ret = f.listFiles(); return handleListFilesResult(f, ret); } /** * Wraps {@link File#listFiles(FilenameFilter)} so it will throw an IOException instead of returning null * @param f directory to list * @param filter see {@link File#listFiles(FilenameFilter)} * @return see {@link File#listFiles(FilenameFilter)}, except this method can never return null * @throws IOException if {@link File#listFiles(FilenameFilter)} returns null */ @Nonnull public static File[] listFiles(File f, FilenameFilter filter) throws IOException { final File[] ret = f.listFiles(filter); return handleListFilesResult(f, ret); } /** * Wraps {@link File#listFiles(FileFilter)} so it will throw an IOException instead of returning null * @param f directory to list * @param filter see {@link File#listFiles(FileFilter)} * @return see {@link File#listFiles(FileFilter)}, except this method can never return null * @throws IOException if {@link File#listFiles(FileFilter)} returns null */ @Nonnull public static File[] listFiles(File f, FileFilter filter) throws IOException { final File[] ret = f.listFiles(filter); return handleListFilesResult(f, ret); } private static <T> T[] handleListFilesResult(File f, T[] result) throws IOException { if (result == null) { if (!f.isDirectory()) { throw new IOException(String.format("Cannot list %s as it is not a directory", f.toString())); } else { throw new IOException(String.format("Cannot list %s", f.toString())); } } return result; } }