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

前端 未结 5 553
不思量自难忘°
不思量自难忘° 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:27

    I have created a sample , you can use the Stream to filter/

    public class ReadFileLines {
        public static void main(String[] args) throws IOException {
            Stream lines = Files.lines(Paths.get("C:/SelfStudy/Input.txt"));
    //      System.out.println(lines.filter(str -> str.contains("SELECT")).count());
    
    //Stream gets closed once you have run the count method.
            System.out.println(lines.parallel().filter(str -> str.contains("Delete")).count());
        }
    }
    

    Sample input.txt.

    SELECT Every thing
    Delete Every thing
    Delete Every thing
    Delete Every thing
    Delete Every thing
    Delete Every thing
    Delete Every thing
    

提交回复
热议问题