日期:2014-05-20 浏览次数:20716 次
import java.io.File; import java.io.FileFilter; public class MyFileTree { public static void main(String[] args) { MyFileTree tree = new MyFileTree(); tree.printFile(new File("c:/windows/system32"), 0); } public void printFile(File f, int indent) { if (f.isFile()) { for (int i = 0; i < indent; i++) { System.out.print(" "); } System.out.print(f.getName()); System.out.println(); } else if (f.isDirectory()) { for (int i = 0; i < indent; i++) { System.out.print(" "); } System.out.print("[" + f.getName() + "]"); System.out.println(); File[] listFiles = f.listFiles(new FileFilter() { public boolean accept(File f) { if (f.isDirectory()) return true; if (f.getName().length() > 4 && f.getName().substring(f.getName().length() - 4) .equalsIgnoreCase(".dll")) { return true; } return false; } }); for (int i = 0; i < listFiles.length; i++) { printFile(listFiles[i], indent + 1); } } else { System.out.println("ERROR!"); } } }
------解决方案--------------------
File f = new File(path);
Files[] files = f.listFiles();
for(int i = 0, l = files.length; i < l; i++){
File f1 = files[i];
}