使用Java 8在目录和子目录中查找文件

时间:2016-12-08 11:08:58

标签: java java-8 directory filesystems nio

如果在给定开始目录和文件名的情况下,如何在 Java 8 中实现算法,该算法搜索给定目录中的文件或嵌套不超过5个级别的任何子目录。

例如,考虑以下目录结构:

Folder 1
   Folder 2
      Folder 3
        Folder 4
            Folder 5
                Folder 6
                    nfiles.txt....
                MyFile.txt
                xfile.txt
            filesInFolder4....
        filesInFolder3...
   .....

该算法应搜索文件夹中包含文件的文件,并报告是否存在具有给定文件名的文件?

如何使用 Java 8

执行此操作

3 个答案:

答案 0 :(得分:11)

请查看Files.find方法。

try (Stream<Path> stream = Files.find(Paths.get("Folder 1"), 5,
            (path, attr) -> path.getFileName().toString().equals("Myfile.txt") )) {
        System.out.println(stream.findAny().isPresent());
} catch (IOException e) {
        e.printStackTrace();
}

答案 1 :(得分:1)

我发现解决方案使用Files.find和Files.walk如下:

// Finding a file upto x level in File Directory using NIO Files.find
    Path start = Paths.get("/Users/***/Documents/server_pull");
    int maxDepth = 5;
    try(Stream<Path> stream = Files.find(start, 
                                        maxDepth, 
                                        (path, attr) -> String.valueOf(path).endsWith(".json"))){
        String fileName = stream
                            .sorted()
                            .map(String::valueOf)
                            .filter((path) -> {
                                //System.out.println("In Filter : "+path);
                                return String.valueOf(path).endsWith("system_health_12_55_TestServer.json");
                            })
                            .collect(Collectors.joining());
        System.out.println("fileName : "+fileName);
    }catch(Exception e){
        e.printStackTrace();
    }

// Finding a file upto x level in File Directory using NIO Files.walk

    Path startWalk = Paths.get("/Users/***/Documents/server_pull");
    int depth = 5;
    try( Stream<Path> stream1 = Files.walk(startWalk, 
                                            depth)){
        String walkedFile = stream1
                            .map(String::valueOf)
                            .filter(path -> {
                                return String.valueOf(path).endsWith("system_health_12_55_TestServer.json");
                            })
                            .sorted()
                            .collect(Collectors.joining());
        System.out.println("walkedFile = "+walkedFile);

    }catch(Exception e){
        e.printStackTrace();
    }

它似乎比walkFileTree更简单......

答案 2 :(得分:0)

我对问题进行了一些深入研究,并找到了一种使用ForkJoinPool以同步方式执行此操作的方法,如下所示:

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ForkJoinPool;
import java.util.concurrent.RecursiveTask;
import java.util.concurrent.TimeUnit;



public class ForkJoinFolderProcessor {

    public static void main(String[] args) {
        ForkJoinPool pool = new ForkJoinPool();


        MyFolderProcessor hadoop = new MyFolderProcessor("/Users/*****/backups/h/", "log");
        MyFolderProcessor t8 = new MyFolderProcessor("/Users/*******/apache-tomcat-9.0.2", "log");
        MyFolderProcessor t9 = new MyFolderProcessor("/Users/******/apache-tomcat-8.5.20", "log");

        pool.execute(hadoop);
        pool.execute(t8);
        pool.execute(t9);

        do {
            System.out.println("---------------------");
            System.out.println("Parallelism : "+pool.getParallelism());
            System.out.println("Active Threads : "+pool.getActiveThreadCount());
            System.out.println("Task Count : "+pool.getQueuedTaskCount());
            System.out.println("Steal Count : "+pool.getStealCount());

            System.out.println("---------------------");

            try
             {
                TimeUnit.SECONDS.sleep(1);
             } catch (InterruptedException e)
             {
                e.printStackTrace();
             }
        }while((!hadoop.isDone()) || (!t8.isDone()) || (!t9.isDone()));

        pool.shutdown();

        List<String> results = hadoop.join();
        System.out.println("Hadoop: Files found  : " + results.size()+" "+results.toString());
        results = t8.join();
        System.out.println("T8: Files found  : " + results.size()+" "+results.toString());
        results = t9.join();
        System.out.println("T9: Files found  : " + results.size()+" "+results.toString());

    }

}

class MyFolderProcessor extends RecursiveTask<List<String>>{

    private static final long serialVersionUID = 1L;

    private final String filepath;
    private final String fileExt;

    public MyFolderProcessor(String path, String extension) {
        this.filepath = path;
        this.fileExt = extension;
    }

    @Override
    protected List<String> compute() {

        List<String> list = new ArrayList<String>();
        List<MyFolderProcessor> tasks = new ArrayList<MyFolderProcessor>();

        File file = new File(filepath);
        File content[] = file.listFiles();

        if(content != null) {
                for(File f : content) {
                    if(f.isDirectory()) {
                        MyFolderProcessor task = new MyFolderProcessor(f.getAbsolutePath(), fileExt);
                        task.fork();
                        tasks.add(task);
                    }else {
                        if(checkFile(f.getName()))
                            list.add(f.getAbsolutePath());
                    }
                }
        }
        if (tasks.size() > 50) {
            System.out.println("tasks ran."+ file.getAbsolutePath()+" "+ tasks.size());
        }
        addResultsFromTasks(list, tasks);

        return list;
    }

    private void addResultsFromTasks(List<String> list, List<MyFolderProcessor> tasks) {
        for (MyFolderProcessor item : tasks) {
            list.addAll(item.join());
        }
    }

    private boolean checkFile(String name) {
        return name.endsWith(fileExt);
    }

}

虽然它是更复杂的解决方案,但在多线程环境下它可以很好地工作。