Java 实例 - 打印目录结构
Java 教程
· 2019-02-11 07:43:28
以下实例演示了使用 File 类的 file.getName() 和 file.listFiles() 方法来打印目录结构:
Main.java 文件
import java.io.File;
import java.io.IOException;
public class FileUtil {
public static void main(String[] a)throws IOException{
showDir(1, new File("d:\\Java"));
}
static void showDir(int indent, File file) throws IOException {
for (int i = 0; i < indent; i++)
System.out.print('-');
System.out.println(file.getName());
if (file.isDirectory()) {
File[] files = file.listFiles();
for (int i = 0; i < files.length; i++)
showDir(indent + 4, files[i]);
}
}
}
以上代码运行输出结果为:
-Java -----codes ---------string.txt ---------array.txt -----w3cschoolcc
点击查看所有 Java 教程 文章: https://www.codercto.com/courses/l/12.html
Java in a Nutshell, 6th Edition
Benjamin J Evans、David Flanagan / O'Reilly Media / 2014-10 / USD 59.99
The latest edition of Java in a Nutshell is designed to help experienced Java programmers get the most out of Java 7 and 8, but it's also a learning path for new developers. Chock full of examples tha......一起来看看 《Java in a Nutshell, 6th Edition》 这本书的介绍吧!