Javaで特定のディレクトリのファイルを反復処理するにはどうすればいいですか? [重複] 質問する

Javaで特定のディレクトリのファイルを反復処理するにはどうすればいいですか? [重複] 質問する

重複の可能性あり:
Java でディレクトリを反復処理する最良の方法は何ですか?

Java を使用して特定のディレクトリ内の各ファイルを処理したいと思います。

これを行う最も簡単な(そして最も一般的な)方法は何ですか?

ベストアンサー1

ディレクトリ名が にある場合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.
  }

おすすめ記事