Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to read file line by line in Java 8?

Tags:

java

java-8

In Java 8 I see new method is added called lines() in Files class which can be used to read a file line by line in Java. Does it work for huge files? I mean can we load first 1000 lines then second set of 1000 lines. I have huge file with 1GB, Will it work?

Could someone share code snippet how to use it?

like image 864
Pratiyush Kumar Singh Avatar asked Jul 24 '15 09:07

Pratiyush Kumar Singh


1 Answers

Does it work for huge files? [...] I have huge file with 1GB, Will it work?

As far as I can see it should work well for big files as well (but I haven't tried):

try(Stream<String> lines = Files.lines(path)){
    lines.filter(...).map(...)....foreach(...);
}

I mean can we load first 1000 lines then second set of 1000 lines.

How many lines are read at one time is implementation specific to Files.lines (which probably uses a BufferedReader, but I might be wrong).

like image 133
Puce Avatar answered Oct 02 '22 19:10

Puce