如何在 Java 中迭代某个目录的文件? [英] How to iterate over the files of a certain directory, in Java?

查看:25
本文介绍了如何在 Java 中迭代某个目录的文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能的重复:
在 java 中遍历目录的最佳方法?

我想使用 Java 处理某个目录中的每个文件.

I want to process each file in a certain directory using Java.

最简单(也是最常见)的方法是什么?

What is the easiest (and most common) way of doing this?

推荐答案

如果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 中迭代某个目录的文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆