java清理指定目录下的空文件夹

import java.io.File;
import java.util.ArrayList;
import java.util.List;
/** *//**
 * **********************************************
 * @description 删除指定目录下的空文件夹
 *         在清除项目业务日志文件时会经常遇到
 * @author Gavin.lee
 * @date Jun 27, 2009    12:24:55 PM
 * @version 1.0
 ***********************************************
 */
public class DeleteNullDirectory {
    /** *//**
     * 列出某文件夹下的空文件夹绝对路径,储存至list
     * @param root
     * @return
     */
    public List getAllNullDirectorys(File root) {
        List list = new ArrayList();
        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]);
                }
                getAllNullDirectorys(dirs[i]);
            }
        }
        return list;
    }
    //删除操作
    public void removeNullFile(List< File> list) {
        for (int i = 0; i < list.size(); i++) {
            File temp = list.get(i);
            if (temp.isDirectory() && temp.listFiles().length <= 0) {
                temp.delete();
            }
        }
    }
    public static void main(String[] args) {
        DeleteNullDirectory m = new DeleteNullDirectory();
        List< File> list = m.getAllNullDirectorys(new File("c:/java/"));
        System.out.println(list.size());
        for (int i = 0; i < list.size(); i++) {
            System.out.println(list.get(i).getPath());
        }
        m.removeNullFile(list);
    }
}

 

你可能感兴趣的:(java)