Possible Duplicate:
Best way to iterate through a directory in java?
我想使用 Java 处理某个目录中的每个文件。
最简单(也是最常见)的方法是什么?
最佳答案
如果myDirectoryPath
中有目录名,
import java.io.File;
...
File dir = new File(myDirectoryPath);
File[] directoryListing = dir.listFiles();
if (directoryListing != null) {
for (File child : directoryListing) {
// Do something with child
}
} else {
// Handle the case where dir is not really a directory.
// Checking dir.isDirectory() above would not be sufficient
// to avoid race conditions with another process that deletes
// directories.
}
关于java - 如何在Java中迭代某个目录的文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4917326/