Java 9 already in full swing and ready for feature complete by end of May. Even though Java 8 released couple of years back, I’m sure there are veryfew companies moved to even Java 8 JDK for their production environment. Few still uses JDK7 and JDK6.
Java 8 by default comes with lots of smart features which I believe we hardly looked at. Sometime back I’ve written an article on Java 8 Stream API Operations and Lambda Expression.
In this tutorial we will go over steps and how we can use Java 8 Stream package to read file content line by line. If you want to go over traditional approach
(pre Java 8) then follow this tutorial which uses FileReader
and BufferReader
utils.
Also, if you have below questions then you are at right place.
- Java 8 Stream of Lines – Read File Line by Line
- Java 8 API Stream by Example
- java – How to read from files with Files.lines(…) and forEach
- Java 8: Reading A File Into A String
- java 8 write stream to file
Let’s get started:
Step-1
Create class CrunchifyJava8StreamReadFile.java
Step-2
We are going to create read line utility using two below approaches. Create different methods for each utility.
lines()
andStream
ApproachnewBufferedReader
andStream
Approach
Step-3
collect
performs a mutable reduction operation
on the elements of this stream using a Collector
. Here is details. I would suggest to go through it thoroughly.
Step-4
We will read file crunchify-java8-stream.txt
with below content. Copy the content and put it into your C drive or Mac’s Downloads folder.
Welcome to Crunchify's JDK 8 Stream API tutorial More than 500 Java and Spring MVC tutorials Company: Crunchify Address: NYC Contact: App Shah
Here is a complete Java Program:
package com.crunchify.tutorials; import java.io.BufferedReader; import java.io.IOException; import java.nio.file.Files; import java.nio.file.Paths; import java.util.ArrayList; import java.util.List; import java.util.stream.Collectors; import java.util.stream.Stream; /** * @author Crunchify.com * */ public class CrunchifyJava8StreamReadFile { public static void main(String args[]) { String crunchifyFile = "/Users/<username>/Downloads/crunchify-java8-stream.txt"; // lines() and Stream Approach CrunchifyReadFile1(crunchifyFile); // newBufferedReader and Stream Approach CrunchifyReadFile2(crunchifyFile); } // Read file using lines() and Stream Approach private static void CrunchifyReadFile1(String crunchifyFile) { Stream<String> crunchifyStream = null; try { // Read all lines from a file as a Stream. Bytes from the file are decoded into characters using the UTF-8 charset crunchifyStream = Files.lines(Paths.get(crunchifyFile)); } catch (IOException e) { e.printStackTrace(); } log("============= Result from lines() and Stream Approach ============="); crunchifyStream.forEach(System.out::println); } // Read file using newBufferedReader and Stream Approach private static void CrunchifyReadFile2(String crunchifyFile) { List<String> crunchifyList = new ArrayList<>(); BufferedReader crunchifyBufferReader = null; try { // newBufferedReader opens a file for reading crunchifyBufferReader = Files.newBufferedReader(Paths.get(crunchifyFile)); } catch (IOException e) { e.printStackTrace(); } // toList: returns a Collector that accumulates the input elements into a new List // lines(): returns a Stream, the elements of which are lines read from this BufferedReader crunchifyList = crunchifyBufferReader.lines().collect(Collectors.toList()); log("\n============= Result from newBufferedReader and Stream Approach ============="); // forEach: performs the given action for each element of the Iterable until all elements have been processed or the // action throws an exception. crunchifyList.forEach(System.out::println); } private static void log(String string) { System.out.println(string); } }
Result/Output:
The post How to Read a File line by line using Java 8 Stream – Files.lines() and Files.newBufferedReader() Utils appeared first on Crunchify.
Author: App Shah