Hello readers, in this tutorial, we will see an example of how to Read a File Line by Line in Java 8. We will learn the Java 8 Stream’s API for reading a file’s content line by line and we will explore its different characteristics.
These days in the programming universe reading the file content is one of the most habitual file manipulation tasks in Java. In the ancient Java world, the code to read the text file line by line was very tedious. In Java8, JDK developers have added new methods to the
java.nio.file.Files class which has opened new gateways for the developers and these new methods offer an efficient reading of the files using the Streams.
Java8 has added the
Files.lines()method to read the file data using the Stream. The attractiveness of this method is that it reads all lines from a file as a stream of strings. This method,
- Reads the file data only after a Terminal operation (such as
count()etc.) is executed on the Stream
- Reads the file content line-by-line by using the Streams
- Works by reading the Byte from a file and then decodes it into a Character using the
Files.lines() is different from the
Files.readAllLines() as the latter one reads all the lines of a file into a list of String elements. This is not an efficient way as the complete file is stored as a List resulting in extra memory consumption.
Java8 also provides another method i.e.
Files.newBufferedReader() which returns a
BufferedReader to read the content from the file. Since both
Files.newBufferedReader() methods return Stream, developers can use this output to carry out some extra processing.
Now, open up the Eclipse Ide and we will go over these methods to read a file line by line using the Java8 Lambda Stream.
2. Java 8 Read File Line by Line Example
2.1 Tools Used
We are using Eclipse Oxygen, JDK 1.8, and Maven.
2.2 Project Structure
Firstly, letâ€™s review the final project structure if you’re confused about where you should create the corresponding files or folder later!
2.3 Project Creation
This section will show how to create a Java-based Maven project with Eclipse. In Eclipse IDE, go to
File -> New -> Maven Project.
In the New Maven Project window, it will ask you to select the project location. By default, ‘Use default workspace location’ will be selected. Select the ‘Create a simple project (skip archetype selection)’ checkbox and just click on the next button to proceed.
It will ask you to ‘Enter the group and the artifact id for the project’. We will input the details as shown in the below image. The version number will be by default:
Click on Finish and the creation of a maven project is completed. If you see, it has downloaded the maven dependencies and a
pom.xml file will be created. It will have the following code:
Developers can start adding the dependencies that they want. Let us start building the application!
3. Application Building
Below are the steps involved in explaining this tutorial.
3.1 Java Class Implementation
Let’s create the required Java files. Right-click on the
New -> Package.
A new pop window will open where we will enter the package name as:
Once the package is created in the application, we will need to create the implementation class to show the
Files.newBufferedReader() methods usage. Right-click on the newly created package:
New -> Class.
A new pop window will open and enter the file name as:
ReadFileLineByLineDemo. The implementation class will be created inside the package:
3.1.1 Example of Reading File in Java8
Here is the complete Java program to read an input file line by line using the Lambda Stream in Java8 programming. In this tutorial we will talk about the 3 different approaches for reading a file:
- Approach 1: This approach (i.e.
fileStreamUsingFiles(……)talks about reading a file into Java8 Stream and printing it line by line
- Approach 2: This approach (i.e.
filterFileData(……)) talks about reading a file into Java8 Stream (like the one we’ll be using in Approach 1) but also apply a filter to it (i.e. Read only those elements from the file that start with an alphabet (
s) and print it onto the console). This approach gives an edge to apply criteria while reading the file
- Approach 3: This approach (i.e.
fileStreamUsingBufferedReader(……)talks about a new Java8 method known as
BufferedReader.lines(……)that lets the
BufferedReaderreturns the content as
- In the above approaches, we have omitted the try-with-resources concept for simplicity and mainly focused on the new ways of reading the file. However, just for developers curiosity try-with-resources is a concept that ensures that each resource is closed at the end of the statement execution
- With enhancements in Java and introduction to Stream in Java8, developers have stopped using the
Scannerclasses to read a file line by line as it does not offer the facilities like the ones offered by Java8 Streams API
Let us move ahead and understand the 3 different approaches with a practical example.
4. Run the Application
To run the application, developers need to right-click on the class,
Run As -> Java Application. Developers can debug the example and see what happens after every step!
5. Project Demo
The above code shows the following logs as output.
That is all for this post. Happy Learning!
6. Java Read File Line by Line – Summary
In this tutorial, we had an in-depth look at:
- Java 8
Files.lines()method in order to read a file line by line lazily and using the Stream API terminal operation (
forEach(……)) to print lines from the file
- Java 8
Files.newBufferedReader()method in order to read a file line by line. This method returns the contents as a
- We also an introduction of Java 8 Stream API methods to make ourselves familiar with this new concept
7. Download the Eclipse Project
This was an example of how to Read a File Line by Line in Java 8.
You can download the full source code of this example here: Java Read File Line by Line Example
Last updated on Apr. 27th, 2020