LoginSignup
2
1

【JavaGold】Filesクラスのlist、walk、findメソッドの比較

Posted at

比較

以下の観点で比較する。

  • 対象のファイルまたはサブディレクトリ
  • 使用方法

対象のファイルまたはサブディレクトリ

  • list():指定されたディレクトリ内の直下のファイルまたはサブディレクトリをリストアップする。サブディレクトリは再帰的には走査しない。

  • walk():指定されたディレクトリ内のファイルやサブディレクトリを再帰的に走査し、全てのエントリをリストアップする。

maxDepthパラメータを使用して、再帰の深さを制御することができる。

  • find():指定されたディレクトリ以下で特定の条件に合致するファイルを再帰的に検索する。
    検索条件はBiPredicate<Path, BasicFileAttributes>インタフェースを使用して指定する。

使用方法

  • list()直下のファイルまたはサブディレクトリをリストアップしたい場合に使用する。

  • walk():ディレクトリ以下の全てのファイルまたはサブディレクトリを再帰的にリストアップしたい場合に使用する。深い階層のファイルを取得することができる。

  • find():ディレクトリ以下で特定の条件に合致するファイルを検索したい場合に使用する。
    検索条件を柔軟に指定できるため、特定のファイルのみを取得することができる。

以下のディレクトリ構造があるとする。

example_directory/
    ├── file1.txt
    ├── subdirectory1/
    │   ├── file2.txt
    │   ├── file3.txt
    │   └── subdirectory2/
    │       ├── file4.txt
    │       └── file5.txt
    └── subdirectory3/
        └── file6.txt

list()の場合

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

public class ListExample {
    public static void main(String[] args) {
        Path dirPath = Paths.get("example_directory");

        try {
            Files.list(dirPath)
                 .forEach(entry -> System.out.println(entry.getFileName()));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
出力結果
file1.txt
subdirectory1
subdirectory3

walk()の場合

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

public class WalkExample {
    public static void main(String[] args) {
        Path dirPath = Paths.get("example_directory");

        try {
            Files.walk(dirPath, Integer.MAX_VALUE)
                 .forEach(entry -> System.out.println(entry.getFileName()));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
出力結果
file1.txt
subdirectory1
file2.txt
file3.txt
subdirectory2
file4.txt
file5.txt
subdirectory3
file6.txt

find()の場合

import java.io.IOException;
import java.nio.file.FileVisitOption;
import java.nio.file.FileVisitResult;
import java.nio.file.FileVisitOption.*;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.EnumSet;

public class FindExample {
    public static void main(String[] args) {
        Path dirPath = Paths.get("example_directory");

        try {
            Files.find(dirPath, Integer.MAX_VALUE,
                       (path, attr) -> path.toString().endsWith(".txt"))
                 .forEach(entry -> System.out.println(entry.getFileName()));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
出力結果
file1.txt
file2.txt
file3.txt
file4.txt
file5.txt
file6.txt

これらの例から、それぞれのメソッドがリストアップするファイルまたはサブディレクトリの範囲が異なることがわかる。

つまり、以下の場合で使い分けることができる。

  • 簡単なファイルのリストアップだけで十分な場合
    list()を使用する。
  • 再帰的に全てをリストアップしたい場合
    walk()を使用する。
  • 特定の条件に合致するファイルを検索したい場合
    find()を使用する。
2
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
1