Java 8 read file line by line examples

Now i am giving you some Java 8 read file line by line examples which are very useful for programmer everyday. Reading files line by line using java IO and perform some operations of lines. By the way, i also mention the classic ways to read using BufferedReader And Scanner.

In this article, i are considering the text file named “test.txt” which are used in all examples.


1. Java 8 read file into stream

In this example, I will read the lines as stream and fetch each line one at a time and check it for string “8”.

One more thing, here i’m using try-with-resources which will take care of the closing the stream.


2. Java 8 Bufferedreader example
Java 8 Bufferedreader is added a new method lines(). It lets BufferedReader returns content as Stream.


3. Classic BufferedReader And Scanner

By the way, we revisit the classic BufferedReader (JDK1.1) and Scanner (JDK1.5) examples to read a file line by line. So you can compare this classic way to the new way of Stream.


That’s all about Java 8 read file line by line examples.

Java 8 File.lines()
Java BufferedReader

Download the complete source code, click link below (11 downloads)

Leave a Comment


Please share it if you found this useful
Hide Buttons