Java Utililty Methods Folder Delete

List of utility methods to do Folder Delete

Description

The list of methods to do Folder Delete are organized into topic(s).

Method

voidclearFolder(String folder)
clear Folder
clearFolder(new File(folder));
booleanclearFolder(String path)
Delete all files in the directory except folders.
boolean flag = true;
File dirFile = new File(path);
if (!dirFile.isDirectory()) {
    return flag;
File[] files = dirFile.listFiles();
for (File file : files) {
    if (file.isFile()) {
...
voidclearFolderContent(String folderName)
clear Folder Content
File folder = new File(folderName);
File[] files = folder.listFiles();
if (files != null) { 
    for (File f : files) {
        if (f.isDirectory()) {
            clearFolderContent(f.getAbsolutePath());
        } else {
            f.delete();
...
voiddeepDelete(final File file)
deep Delete
if (!file.exists())
    return;
if (file.isDirectory()) {
    if (file.list().length == 0) {
        file.delete();
    } else {
        final String files[] = file.list();
        for (final String temp : files) {
...
voidemptyAndDelete(File fileOrDir)
Deletes a file or directory.
if (fileOrDir.isDirectory()) {
    File[] files = fileOrDir.listFiles();
    for (File file : files) {
        emptyAndDelete(file);
fileOrDir.delete();
voidemptyDerbyTestDirectory(final String derbyTestDirectory)
Clean out the test directory.
File tempDirectory = new File(derbyTestDirectory);
File[] files = tempDirectory.listFiles();
if (files != null) {
    for (File file : files) {
        if (file.isDirectory()) {
            File[] subFiles = file.listFiles();
            for (File subFile : subFiles) {
                if (subFile.isDirectory()) {
...
voidemptyFile(final String urlFile)
Empty a file.
final File file = new File(urlFile);
if (file.exists()) {
    if (!file.delete()) {
        throw new IOException("Failed to delete file : " + file);
if (!file.createNewFile()) {
    throw new IOException("Failed to create file : " + file);
...
voidemptyFile(String srcFilename)
empty File
File srcFile = new File(srcFilename);
if (!srcFile.exists()) {
    throw new FileNotFoundException("Cannot find the file: " + srcFile.getAbsolutePath());
if (!srcFile.canWrite()) {
    throw new IOException("Cannot write the file: " + srcFile.getAbsolutePath());
FileOutputStream outputStream = new FileOutputStream(srcFilename);
...
booleanemptyFolder(File dir)
Remove all files from a folder, return false if at least one file could not be deleted.
if (dir == null)
    throw new IllegalArgumentException("Null path in FileUtil.emptyFolder()");
if (!dir.isDirectory())
    throw new IllegalArgumentException(
            "Invalid path " + dir.getAbsolutePath() + ": not a directory, in FileUtil.emptyFolder()");
boolean ok = true;
File[] files = dir.listFiles();
for (int i = 0; i < files.length && ok; i++) {
...
voidemptyFolder(File folder)
empty Folder
if (folder.isDirectory()) {
    File[] files = folder.listFiles();
    for (File file : files) {
        if (file.isDirectory()) {
            emptyFolder(file);
        } else {
            file.delete();