How to read from files with Files.lines(…).forEach(…)?

前端 未结 5 551
不思量自难忘°
不思量自难忘° 2020-12-03 07:53

I\'m currently trying to read lines from a text only file that I have. I found on another stackoverflow(Reading a plain text file in Java) that you can use Files.lines(..).f

5条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-03 08:16

    Files.lines(Path) expects a Path argument and returns a Stream. Stream#forEach(Consumer) expects a Consumer argument. So invoke the method, passing it a Consumer. That object will have to be implemented to do what you want for each line.

    This is Java 8, so you can use lambda expressions or method references to provide a Consumer argument.

提交回复
热议问题