package com.swift.kuozhan; import java.io.File; import java.io.FileFilter; /*使用文件过滤器筛选将指定文件夹下的小于200K的小文件获取并打印(包括所有子文件夹的文件)。*/ public class kuaozhan1 { public static void main(String[] args) { File dir = new File("c:/"); if(!dir.exists()) { throw new RuntimeException("该文件夹不存在"); } get200(dir, 0); } private static void get200(File dir, int length) { if(!dir.exists()) { throw new RuntimeException("该文件夹不无访问权限"); } for (int i = 0; i < length; i++) System.out.print("\t"); System.out.println(dir); File[] files = dir.listFiles(new FileFilter() { @Override public boolean accept(File pathname) { if (pathname.isDirectory()) return true; if (pathname.isHidden()) return false; if (pathname.length() / 1024 < 200) return true; return false; } }); for (File file : files) { if (file.isDirectory()) { get200(file, length + 1); } else { for (int i = 0; i < length; i++) System.out.print("\t"); System.out.println("\t" + file); } } } }