Java read file line by line With Examples

Today we will look into different java read file line by line methods. Sometimes we have to read file line by line to a String, for example calling a method by passing each line as String to process it.

Java Read File line by line

java-read-file-line-by-line

We can read file line by line using different ways. Let’s look at some of them.

Java Read File line by line using BufferedReader

We can use java.io.BufferedReader readLine() method to read file line by line to String. This method returns null when end of file is reached. Below is a simple program showing example for java read file line by line using BufferedReader.

Java Read File line by line using Scanner

We can use Scanner class to open a file and then read its content line by line. Below is the scanner example to read file line by line and print it.

Java Read File line by line using Files

java.nio.file.Files is a utility class that contains various useful methods. Files readAllLines method can be used to read all the file lines into a list of string.

Java Read File line by line using RandomAccessFile

We can use RandomAccessFile to open a file in read mode and then use its readLine method to read file line by line.

That’s all for java read file line by line using different methods.

References:

By admin

Leave a Reply