java - 如何在Java中迭代某个目录的文件?

标签 java file directory

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/

相关文章:

php - Mime Type 和文件扩展名有什么关系

java - Prerender.io 与 Play 框架

java - 用于在 Java 中保存访问 token 的线程安全类

java - 关于理解和使用网络服务的一些简单建议?

ruby - 使用 File.open 从 ruby​​ 中的目录打开文件

perl - 如何检测 Perl 中不断变化的目录大小

directory - 从 AppleScript 路径中提取文件扩展名

java - 将 PinField.getPassword() 的输出转换为字符串

file - 如何删除所有KDevelop生成的文件?

c - 从 C 中的文本文件读取整数时的错误