Java File Path Delete delAllFile(String path)

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

Description

delete all files inside folder

License

Apache License

Parameter

Parameter Description
path String folder path

Declaration

public static void delAllFile(String path) 

Method Source Code


//package com.java2s;
//License from project: Apache License 

import java.io.*;

public class Main {
    /**/*from   w  w w  . j av  a 2 s .  com*/
     * delete all files inside folder
     *
     * @param path String folder path
     */
    public static void delAllFile(String path) {
        File file = new File(path);
        if (!file.exists()) {
            return;
        }
        if (!file.isDirectory()) {
            return;
        }
        if (file.getAbsolutePath().equalsIgnoreCase("/")) {
            System.out.println("this is a root directory, you cannot delete all files in it!");
            System.out.println("please change the path!");
            return;
        }
        if (file.getAbsolutePath().equalsIgnoreCase("/root")) {
            System.out.println("this is a root directory, you cannot delete all files in it!");
            System.out.println("please change the path!");
            return;
        }
        if (file.getAbsolutePath().equalsIgnoreCase("/usr") || file.getAbsolutePath().equalsIgnoreCase("/opt")
                || file.getAbsolutePath().equalsIgnoreCase("/bin")
                || file.getAbsolutePath().equalsIgnoreCase("/sbin")
                || file.getAbsolutePath().equalsIgnoreCase("/etc")
                || file.getAbsolutePath().equalsIgnoreCase("/selinux")
                || file.getAbsolutePath().equalsIgnoreCase("/sys")
                || file.getAbsolutePath().equalsIgnoreCase("/var")
                || file.getAbsolutePath().equalsIgnoreCase("/home")
                || file.getAbsolutePath().equalsIgnoreCase("/net")) {
            System.out.println("this is a root directory, you cannot delete all files in it!");
            System.out.println("please change the path!");
            return;
        }
        if (file.getAbsolutePath().equalsIgnoreCase("C://") || file.getAbsolutePath().equalsIgnoreCase("C:\\\\")) {
            System.out.println("this is a root directory, you cannot delete all files in it!");
            System.out.println("please change the path!");
            return;
        }
        String[] tempList = file.list();
        File temp;
        if (tempList == null) {
            return;
        }
        for (String aTempList : tempList) {
            if (path.endsWith(File.separator)) {
                temp = new File(path + aTempList);
            } else {
                temp = new File(path + File.separator + aTempList);
            }
            if (temp.isFile()) {
                temp.delete();
            }
            if (temp.isDirectory()) {
                delAllFile(path + "/" + aTempList);// delete all files inside
                delFolder(path + "/" + aTempList);// delete the empty folder
            }
        }
    }

    /**
     * delete folder
     *
     * @param folderPath String folder path
     */
    public static void delFolder(String folderPath) {
        try {
            delAllFile(folderPath); // delete all files inside
            File file = new File(folderPath);
            file.delete(); // delete the empty folder
        } catch (Exception e) {
            System.out.println("delete folder failed");
            e.printStackTrace();
        }
    }
}

Related

  1. delAll(String path)
  2. delAllFiles(File dir, String prefix)
  3. delAllFiles(File dir, String suffix)
  4. delDir(String path)
  5. delDirAndFile(String tempath)