Read file line by line in Java 8

In this tutorial we are going to teach you to use the Files class of Java 8 for reading a file line by line.

Read file line by line in Java 8

In this tutorial we are going to teach you to use the Files class of Java 8 for reading a file line by line.

Read file line by line in Java 8

Java 8 Read file - Reading file line by line in Java 8

Today  we will discuss the new API which is added to Java 8 for reading a file line by line. The program discussed here is reading file using this new API and it reads the file using the Buffered stream making it efficient program. This version of example code will use the minimum memory as it reads file line by line and there will be no out of memory exception in your program.

Adding of new methods to the java.nio.file.Files class to achieve the efficient reading to files is a good feature of Java 8.

In Java 8 the class java.nio.file.Files was modified and new methods were added to enable the class to read files using stream.

In Java 8 updates Files class is present in the java.nio.file package. Here are the details of method which is used for reading file in Java:

Class: java.nio.file.Files

This class is used for performing various operations on the files and directories.

The java.nio.file.Files class provides static methods that operate on files, directories and any other file type.

Files class of Java 8 provides following methods which can be used for reading file line by line:

Files.lines():

This method is used to read all lines from a file using the stream. This method is used to read file line by line with the use of stream which makes it an efficient class for reading file line by line.

The lines() method of Files class works by reading Byte from the file and then reads into character using the UTF-8 character encoding.

Examples of reading a file line by line in Java 8

Following is example of reading file line by line using the Files.lines() method:

package net.roseindia;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;

public class ReadWithFilesLines {

	public static void main(String[] args) {
	System.out.println("Java 8 Read File Example with Files.lines() function");
	
	try {
		
	Files.lines(new File("data.txt").toPath())
		.map(s -> s.trim())
		.filter(s -> !s.isEmpty())
		.forEach(System.out::println);
		
	} catch (IOException e) {
		e.printStackTrace();
	}		

	}

}

In the above example code we are using the Files.lines() function line by line and then prints the data on the console.

Reading file by applying the filter in Java 8

Following is the example of reading file line by line in Java 8 using the function Files.lines() by applying the filter:

package net.roseindia;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;

public class ReadWithFilesLinesWithFilters {

	public static void main(String[] args) {
	System.out.println("Example of Reading file in Java 8 using Files.lines() by applying filter");
	
	try {
		Files.lines(Paths.get("data.txt")).forEach(System.out::println);
	} catch (IOException e) {
		e.printStackTrace();
	}		

	}

}

More examples of reading files in Java: