Java Example - Traverse Directory
以下实例演示了使用 File 类的 dir.isDirectory() 和 dir.list() 方法来遍历目录:
/* author by w3cschool.cc Main.java */ import java.io.File; public class Main { public static void main(String[] argv) throws Exception { System.out.println("遍历目录"); File dir = new File("/www/java"); //要遍历的目录 visitAllDirsAndFiles(dir); } public static void visitAllDirsAndFiles(File dir) { System.out.println(dir); if (dir.isDirectory()) { String[] children = dir.list(); for (int i = 0; i < children.length; i++) { visitAllDirsAndFiles (new File(dir, children[i])); } } } }
以上代码运行输出结果为:
遍历目录 /www/java /www/java/Car.class /www/java/FileUtil.class /www/java/FileUtil.java /www/java/HelloWorld.class /www/java/HelloWorld.java /www/java/HelloWorldDebug.class /www/java/HelloWorldDebug.java /www/java/Main$1.class /www/java/Main.class /www/java/Main.java /www/java/MainClass.class /www/java/MainClass.java /www/java/MyClass.class /www/java/outfilename /www/java/test.log
以上就是Java 实例 - 遍历目录的内容,更多相关内容请关注PHP中文网(www.php.cn)!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Guide to Square Root in Java. Here we discuss how Square Root works in Java with example and its code implementation respectively.

Guide to Perfect Number in Java. Here we discuss the Definition, How to check Perfect number in Java?, examples with code implementation.

Guide to Random Number Generator in Java. Here we discuss Functions in Java with examples and two different Generators with ther examples.

Guide to Weka in Java. Here we discuss the Introduction, how to use weka java, the type of platform, and advantages with examples.

Guide to the Armstrong Number in Java. Here we discuss an introduction to Armstrong's number in java along with some of the code.

Guide to Smith Number in Java. Here we discuss the Definition, How to check smith number in Java? example with code implementation.

In this article, we have kept the most asked Java Spring Interview Questions with their detailed answers. So that you can crack the interview.

Java 8 introduces the Stream API, providing a powerful and expressive way to process data collections. However, a common question when using Stream is: How to break or return from a forEach operation? Traditional loops allow for early interruption or return, but Stream's forEach method does not directly support this method. This article will explain the reasons and explore alternative methods for implementing premature termination in Stream processing systems. Further reading: Java Stream API improvements Understand Stream forEach The forEach method is a terminal operation that performs one operation on each element in the Stream. Its design intention is
