Java 实例 - 遍历指定目录下的所有目录

Java 教程 · 2019-02-11 07:59:27

以下实例演示了如何使用 File 类的 list 方法来遍历指定目录下的所有目录:

Main.java 文件

import java.io.*; class Main { public static void main(String[] args) { File dir = new File("F:"); File[] files = dir.listFiles(); FileFilter fileFilter = new FileFilter() { public boolean accept(File file) { return file.isDirectory(); } }; files = dir.listFiles(fileFilter); System.out.println(files.length); if (files.length == 0) { System.out.println("目录不存在或它不是一个目录"); } else { for (int i=0; i< files.length; i++) { File filename = files[i]; System.out.println(filename.toString()); } } } }

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

14
F:\C Drive Data Old HDD
F:\Desktop1
F:\harsh
F:\hharsh final
F:\hhhh
F:\mov
F:\msdownld.tmp
F:\New Folder
F:\ravi
F:\ravi3
F:\RECYCLER
F:\System Volume Information
F:\temp
F:\work

点击查看所有 Java 教程 文章: https://www.codercto.com/courses/l/12.html

查看所有标签

Programming the Mobile Web

Programming the Mobile Web

Maximiliano Firtman / O'Reilly Media / 2010-07-23 / $44.99

* Learn how to use your existing skills to move into mobile web development * Discover the new possibilities of mobile web development, and understand its limitations * Get detailed coverage of ......一起来看看 《Programming the Mobile Web》 这本书的介绍吧!

CSS 压缩/解压工具
CSS 压缩/解压工具

在线压缩/解压 CSS 代码

Markdown 在线编辑器
Markdown 在线编辑器

Markdown 在线编辑器

RGB HSV 转换
RGB HSV 转换

RGB HSV 互转工具