Java File Path Delete deleteFileSystemDirectory(String dirPath)

Here you can find the source of deleteFileSystemDirectory(String dirPath)

Description

delete File System Directory

License

Open Source License

Declaration

public static void deleteFileSystemDirectory(String dirPath) 

Method Source Code


//package com.java2s;
import java.io.*;

public class Main {
    public static void deleteFileSystemDirectory(String dirPath) {
        deleteFileSystemDirectory(new File(dirPath));
    }//from   www  .  j a v  a  2  s  .co m

    public static void deleteFileSystemDirectory(File current) {
        File[] files = current.listFiles();

        for (int i = 0; files != null && i < files.length; i++) {
            File file = files[i];
            if (file.isDirectory())
                deleteFileSystemDirectory(file);
            else
                deleteFile(file);
        }
        deleteFile(current);
    }

    public static void deleteFile(String filename) {
        deleteFile(new File(filename));
    }

    public static void deleteFile(File file) {
        if (!file.exists())
            return;
        if (!file.delete())
            throw new RuntimeException("Could not delete '" + file.getAbsoluteFile() + "'");
        waitUntilFileDeleted(file);
    }

    private static void waitUntilFileDeleted(File file) {
        int i = 10;
        while (file.exists()) {
            if (--i <= 0) {
                System.out.println("Breaking out of delete wait");
                break;
            }
            try {
                Thread.sleep(500);
            } catch (InterruptedException e) {
            }
        }
    }
}

Related

  1. deleteFiles(String filePath)
  2. deleteFilesInDirectory(File path)
  3. deleteFilesInDirectory(String pathname)
  4. deleteFilesinPath(File pBaseDir, String pFileName)
  5. deleteFilesRecursive(final File path)
  6. deleteFileSystemDirectory(String dirPath)
  7. deleteFileWithoutException(final String path)
  8. deleteFileWithSuffix(String path, String suffix)
  9. deleteFolder(File path)