列出目录下对应的子目录及文件

//递归实现获取对应目录下的子目录及文件

1
package com.lixl.test; 2 3 import java.io.*; 4 5 public class FileList { 6 7 public static void main(String[] args){ 8 9 File f = new File("D:/eclipse"); 10 int level = 0; 11 12 System.out.println(f.getName()); 13 level++; 14 listFile(f,level); 15 } 16 17 private static void listFile(File f,int level){ 18 19 String preString = ""; 20 for(int i = 0;i < level ;i++){ 21 preString += "|____"; 22 } 23 24 File[] childFile = f.listFiles(); 25 26 for(int i = 0;i < childFile.length;i++){ 27 System.out.println(preString + childFile[i].getName()); 28 if(childFile[i].isDirectory()){ 29 level++; 30 listFile(childFile[i],level); 31 } 32 } 33 } 34 }
posted @ 2012-08-02 22:57  cnlixl  阅读(263)  评论(0编辑  收藏  举报