Discuss / Java / 作业

作业

Topic source

小风

#1 Created at ... [Delete] [Delete and Lock User]
package com.itranswarp.learnjava;
import java.io.File;
import java.io.IOException;
/**
 * Learn Java from https://www.liaoxuefeng.com/
 * 
 * @author liaoxuefeng
 */
public class Main {
	static int n =1;
	public static void main(String[] args) throws IOException {
		File currentDir = new File("D:\\Documents\\快捷手册\\jdk-21.0.1_doc-all\\docs");
		listDir(currentDir.getCanonicalFile(),1);
	}
	static void listDir(File dir,int blank) {
		File[] fs = dir.listFiles();
		if (fs != null) {
			for (File f : fs) {
				if(f.isDirectory()) {
					System.out.println(f.getName());
					n++;
					listDir(f,n);
				}else {
					n = 1;
					for (int i = 0; i < blank; i++) {
						System.out.print(" ");
					}
					System.out.println(f.getName());
				}
				
			}
		}
	}
}


  • 1

Reply