Java - Remove Directory and Files

Environment
  • Java


Introduction
Remove directory and files



Code
    /*
     * Remove directory and files
     * If this is a directory and have files, delete them also
     * @param String path
     * @return boolean
     */
    public static boolean rmDir(String path) {
        // Get a conflict if there is windows environment,
        // therefore, replace "\" to be "/"
        path = path.replace('\\', '/');
       
        File file = new File(path);
       
        if (file.exists()) {
            File[] files = file.listFiles();

            for (int i = 0; i < files.length; i++) {
                if (files[i].isDirectory()) {
                    FileDirectory.rmDir(files[i].getName());
                } else {
                    files[i].delete();
                }
            }
        }

        return file.delete();
    }