Java递归列出所有文件和文件夹

package file_op;

import java.io.File;

public class file_list {
	
	static int n =0;

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		
		String ifile = "D:\\";
		System.out.println(ifile);
		list(ifile);

	}
	
	public static void list(String sfile){
		
		n++;
		File f = new File(sfile);
		File[] l = f.listFiles();
		for (int i=0; i<l.length; i++){
			for(int j=0; j<n; j++){
				System.out.print("   ");
			}
			if (l[i].isFile()){				
				System.out.println(l[i].getName());
			}
			else if (l[i].isDirectory()){
				System.out.println( l[i].getName() + '\\');
				list(l[i].getAbsolutePath());
			}
		}
		n--;
		
	}

}

 

posted @ 2014-11-11 21:46  MSTK  阅读(546)  评论(0编辑  收藏  举报