Java File Path Delete deleteFolder(String path)

Here you can find the source of deleteFolder(String path)

Description

delete Folder

License

Open Source License

Declaration

public static void deleteFolder(String path) 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.io.File;

public class Main {

    public static void deleteFolder(String path) {
        File file = new File(path);
        if (file.exists()) {
            if (file.isFile()) {
                deleteFile(path);/*from www.  j  av a2  s .co  m*/
            } else {
                deleteDirectory(path);
            }
        }
    }

    private static void deleteFile(String filePath) {

        File file = new File(filePath);

        if (file.isFile() && file.exists())
            file.delete();
    }

    private static void deleteDirectory(String filePath) {
        if (!filePath.endsWith(File.separator)) {
            filePath = filePath + File.separator;
        }
        File file = new File(filePath);
        if (!file.exists() && !file.isDirectory()) {
            return;
        } else {

            File[] fl = file.listFiles();
            for (int i = 0; i < fl.length; i++) {
                if (fl[i].isFile()) {
                    deleteFile(fl[i].getAbsolutePath());
                } else {
                    deleteDirectory(fl[i].getAbsolutePath());
                }
            }
        }
        file.delete();

    }
}

Related

  1. deleteFolder(File path)
  2. deleteFolder(String filePath)
  3. deleteFolder(String folderPath)
  4. deleteFolder(String folderPath)
  5. deleteFolder(String folderPath)
  6. deleteFolder(String path)
  7. deleteFolder(String path)
  8. deleteFolder(String path)
  9. deleteFolder(String path)