This article describes the method of deleting all empty folders in a specified directory by Java. Share it for your reference, as follows:
package com.func;import java.io.File;import java.util.ArrayList;import java.util.List;/** * Delete all empty folders in the specified directory* * @author zdw * */public class FileUtils{ List<File> list = new ArrayList<File>(); // Get all folders in a certain directory public List<File> visitAll(File root) { File[] dirs = root.listFiles(); if (dirs != null) { for (int i = 0; i < dirs.length; i++) { if (dirs[i].isDirectory()) { System.out.println("name:" + dirs[i].getPath()); list.add(dirs[i]); } visitAll(dirs[i]); } } return list; } /** * Delete empty folder* @param list */ public void removeNullFile(List<File> list) { for (int i = 0; i < list.size(); i++) { File temp = list.get(i); // It is a directory and is empty if (temp.isDirectory() && temp.listFiles().length <= 0) { temp.delete(); } } } /** * @param args */ public static void main(String[] args) { FileUtils m = new FileUtils(); List<File> list = m.visitAll(new File("e:/aaa")); System.out.println(list.size()); for (int i = 0; i < list.size(); i++) { System.out.println(list.get(i).getPath()); } m.removeNullFile(list); System.out.println("ok"); }}For more information about Java related content, please check out the topics of this site: "Summary of Java Files and Directory Operation Skills", "Tutorial on Java Data Structures and Algorithms", "Summary of Java Operation DOM Node Operation Skills" and "Summary of Java Cache Operation Skills"
I hope this article will be helpful to everyone's Java programming.