Is there any way for reading two or more files in one Java8-stream?

前端 未结 3 1405
無奈伤痛
無奈伤痛 2021-01-02 21:52

I like new Java8 StreamAPI and want use it not only for one file. As usually, I use this code:

Stream lines = Files.lines(Paths.get(\"/somepat         


        
3条回答
  •  遥遥无期
    2021-01-02 22:14

    You can use following code

    Files.list(Paths.get("path"))
                .filter(Files::isRegularFile)
                .flatMap(s -> {
                    try {
                        return Files.lines(s);
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                    return null;
                })
                .forEach(System.out::println);
    

提交回复
热议问题