Java read file line by line

In this post, we will see different ways to read file line by line in java.

Sometimes, we need to read file line by line to a String, and process it.

Here are different ways to read file line by line in java.

Java 8 Streams

Java 8 has introduced a new method called lines() in Files class which can be used to read file line by line.

The advantage of using this approach is that it will return Stream of String which will be populated lazily as Stream is used. In case, you need to read the large file and require only first 1000 lines to read, then this is the best approach as it will not rest of the file in memory which will result in better throughput.

Another advantage is that you can use different Stream methods to perform the operation on Stream such as filter, map, etc.

Let’s see this with the help of an example.

Let’s say you want print all male employees.You can use filter method to filter all the male employees.

Using BufferReader

You can use java.io.BufferReader‘s readLine() method to read file into String line by line.Once you reach end of the file, it will return null.

Using Scanner

You can use java.util.Scanner class to read file line by line. You can use hasNextLine() method to check if there are more lines to read and nextLine() to retrieve current line and move the read position to next line.

Scanner class divides its input into token using delimiter, which is either line feed("\n") or carriage return("\r".) in our case.

Using Files

You can use java.nio.file.File ‘s readAllLines method to read file into list of String.

It may not perform well in case you have large file.

Using RandomAccessFile

You can also use RandomAccessFile's readLine() method to read file line by line in java. You need to open file in read mode and then call readLine() method to read file line by line.

Using Apache Commons

In case you prefer to use Apache commons library, you can use FileUtils‘s readLines method to read file into the list.

You need to include following dependencies in pom.xml.

Please note that this approach may not be well optimized. It will read the complete file into a list which can perform badly in case of large files.

That’s all about how to read file line by line in java.

Related Posts

  • 27 December

    How to get current working directory in java

    Learn about how to get current working directory in java using different ways

  • 08 August

    Difference between Scanner and BufferReader in java

    Table of ContentsIntroductionScannerBufferedReaderDifference between Scanner and BufferedReader In this post, we will see difference between Scanner and BufferReader in java. Java has two classes that have been used for reading files for a very long time. These two classes are Scanner and BufferedReader. In this post, we are going to find major differences and similarities […]

  • 29 March

    Read UTF-8 Encoded Data in java

    Table of ContentsUsing Files’s newBufferedReader()Using BufferedReaderUsing DataInputStream’s readUTF() method In this post, we will see how to read UTF-8 Encoded Data. Sometimes, we have to deal with UTF-8 Encoded Data in our application. It may be due localization or may be processing data from user input. There are multiple ways to read UTF-8 Encoded Data […]

  • 29 March

    Write UTF-8 Encoded Data in java

    Table of ContentsUsing Files’s newBufferWriter()Using BufferedWriterUsing DataOutputStream’s writeUTF() method In this post, we will see how to write UTF-8 Encoded Data. Sometimes, we have to deal with UTF-8 Encoded Data in our application. It may be due localization or may be processing data from user input. We will use Hindi language sentences to write in […]

  • 06 February

    Java FileWriter Example

    Table of ContentsConstructorsMethodsExampleCode:Output: The FileWriter class of java.io package makes writing to files in Java as easy as A-B-C. The FileWriter class writes to files as a stream of characters instead of bytes(as in FileOutputStream). Like its input-oriented counterpart FileReader, the FileWriter class is intended for writing “text” based files one character at a time. […]

  • 05 October

    Java FileReader Example

    Table of ContentsConstructorsMethodsExampleCode:Output: The FileReader class of java.io package provides a seamless and easy-to-use way to read and analyse the contents of a file using Java. The FileReader class, works similar to the FileInputStream class because it reads the file data as a stream. However, unlike the latter which reads files as a stream of […]

Leave a Reply

Your email address will not be published. Required fields are marked *

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.