java如何扫描指定路径下的所有文件夹及文件

import java.io.File;

public class GetAllFiles {
	static int count = 0;
	public static void main(String[] args) {
		String path = "C:" + File.separator + "Users" + File.separator
                + "22162" + File.separator + "Desktop" + File.separator + "网申资料";
		System.out.println(path);
        File file = new File(path);
        //io操作最好放到一个线程里去
        new Thread(()->{
            long start = System.currentTimeMillis();
            listAllFiles(file);
            long end = System.currentTimeMillis();
            System.out.println("共花费:" + (end - start) + "毫秒");
            System.out.println("有" + count + "个文件");
        }).start();
	}
	public static void listAllFiles(File file){
        if (file.isFile()){
            count++;
            System.out.println(file);
        } else {
            if (file.exists() && file.isDirectory()){
                File[] files = file.listFiles();
                for (File file1 : files){
                    listAllFiles(file1);
                }
            }
        }
    }

}

你可能感兴趣的:(java)