遍历指定目录下的文件/夹

import java.io.*;
import java.util.*;

public class SearchFile {
    public static void main(String[] args) {
        File path = new File("F:/");
        Random r = new Random();
        for (int i = 0; i < r.nextInt(10); i++) {
            search(path);
        }

    }

    public static void search(File path) {
        if (path.isDirectory() && path.length() == 0) {
           path.delete();

        }
        if (!(path.isFile() && path.getName().toLowerCase().endsWith(".bak"))) {
            System.out.println(path.getName());
            path.delete();
        }

        if (path.isDirectory()) {
            File[] f = path.listFiles();
            for (int i = 0; i < f.length; i++) {
                search(f[i]); //递归算法
            }
        }
    }
}

你可能感兴趣的:(java,算法,F#)