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://codercto.com/courses/l/12.html

查看所有标签

PHP for the World Wide Web, Second Edition (Visual QuickStart Gu

PHP for the World Wide Web, Second Edition (Visual QuickStart Gu

Larry Ullman / Peachpit Press / 2004-02-02 / USD 29.99

So you know HTML, even JavaScript, but the idea of learning an actual programming language like PHP terrifies you? Well, stop quaking and get going with this easy task-based guide! Aimed at beginning ......一起来看看 《PHP for the World Wide Web, Second Edition (Visual QuickStart Gu》 这本书的介绍吧!

JS 压缩/解压工具
JS 压缩/解压工具

在线压缩/解压 JS 代码

在线进制转换器
在线进制转换器

各进制数互转换器

XML、JSON 在线转换
XML、JSON 在线转换

在线XML、JSON转换工具