java递归列出文件中所有文件名称

import java.io.*;

public class FileList {
	
	private static int leval = 1;
	
	public static void main(String[] args) {
		File file = new File("D:/test");
		System.out.println(file.getName());
		fileList(file, leval);
	}
	
	private static void fileList(File file, int listLeval) {
		String preString = "";
		for(int l = 0; l < listLeval; l++) {
			preString += "	";
		}
		File[] fileChilds = file.listFiles();
		for(int i = 0; i < fileChilds.length; i++) {
			System.out.println(preString + fileChilds[i].getName());
			if(fileChilds[i].isDirectory()) {
				fileList(fileChilds[i], ++listLeval);
			}
		}
	}
}

猜你喜欢

转载自blog.csdn.net/weixin_41996632/article/details/84927128