Home > Java > javaTutorial > body text

Java Example - Print Directory Structure

黄舟
Release: 2017-02-13 10:18:58
Original
1546 people have browsed it

以下实例演示了使用 File 类的 file.getName() 和 file.listFiles() 方法来打印目录结构:

/*
 author by w3cschool.cc
 FileUtil.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(&#39;-&#39;);
      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]);
      }
   }}
Copy after login

以上代码运行输出结果为:

-Java
-----codes
---------string.txt
---------array.txt
-----w3cschoolcc
Copy after login

以上就是Java 实例 - 打印目录结构的内容,更多相关内容请关注PHP中文网(www.php.cn)!


source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template