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

时间:2022-07-07 16:25:51
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--; } }