Java Program to Read a Large Text File Line By Line


This article includes the way to read text files line by line. Here, the three different methods are explained with examples. Storing a file in text format is important especially when data from one structured /unstructured form is transferred to another form. Therefore, basic file transfer systems integrate the txt file reading and writing process as their application components. Therefore, how to use text file reading and writing methods is also important for making the parsers.

Multiple Approaches

The given problem statement is solved in three different approaches

  • By using the BufferedReader class

  • By using the FileInputStream

  • By using the FileReader

Let’s see the program along with its output sequentially.

Note − These programs will not give the expected result on any online Java compiler. As online editors will not access your local system’s file path.

Approach 1:- By using the BufferedReader

In this approach, the buffer is provided by the BufferedReader into the FileReader. Its performance is effective as it uses larger block reading rather than a single character reading at a time.

Algorithm

  • Step 1 − Specify the file to be read.

  • Step 2 − Read the file lines as per distinct approaches that means the file is read as a block.

  • Step 3 − Print the lines read from the file on the display/screen.

Explanation

BufferReader is not the same as BufferedInputStream because BufferedReader reads characters while BufferedInputStream reads raw bytes. Reader is the superclass of BufferedReader and the super class of Reader class is the Object class. The BufferedReader class supports two constructors. Using these constructors bufferedReader instances are created. One constructor accepts the reader instance and the other constructor accepts the reader instance and the buffer size.

Example (Approach 1)

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
public class Read {
   public static void main(String[] args) {
      
      // making the BufferedReader object
      BufferedReader b_reader;
      try {
      
         // Reading the sample.txt file
         b_reader = new BufferedReader(new FileReader("sample.txt"));
         
         //reading line by line
         String ln = b_reader.readLine();
         while (ln != null) {
      
            //printing the line
            System.out.println(ln);
            ln = b_reader.readLine();
         }
         b_reader.close();
      } catch (IOException e) {
         e.printStackTrace();
      }
   }
}

Output

C:\java\javaprgstu\filehandling\File Handling Tutorial\Code\1_ReadFiles>javac Read.java
C:\java\javaprgstu\filehandling\File Handling Tutorial\Code\1_ReadFiles>java Read
Block1
This is an experimental file. Inserting the line 100
This is an experimental file. Inserting the line 101
This is an experimental file. Inserting the line 102
…..
This is an experimental file. Inserting the line 277
Block2
This is an experimental file. Inserting the line 100
This is an experimental file. Inserting the line 101
….
This is an experimental file. Inserting the line 277
Block3
This is an experimental file. Inserting the line 100
…
This is an experimental file. Inserting the line 277

The Sample File used contains 3 Blocks of Lines with 177 lines in each block

Approach 2:- By using the FileInputStream

Java Applications can use FileInputStream to read data from the text files. In this approach, the file content is read as a bytes stream.

Algorithm

  • Step 1 − Specify the file to be read.

  • Step 2 − Retrieve the file lines and the file is read as a bytes.

  • Step 3 − Display the lines read from the file on the screen.

Explanation

  • InputStream is the superclass of FileInputStream.

  • The FileInputStream class supports three constructors. Using these constructors FileInputStream instances are created. A constructor accepts the String as a parameter and the other constructor accepts the file object as a parameter.

Example (Approach 2)

import java.io.FileInputStream;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.util.Scanner;
import java.io.FileNotFoundException;
public class Read2 {
   public static void main(String[] args)
   
   // exception handling
   throws FileNotFoundException{
      String path = "C:\java\javaprgstu\filehandling\File Handling Tutorial\Code\1_ReadFiles\sample.txt";
   
      //passing the file as a parameter to FileInputStream
      InputStream ins = new FileInputStream(path);
      
      // using Scanner scn to read the file input stream
      try (Scanner scn = new Scanner(
      ins, StandardCharsets.UTF_8.name())) {
         while (scn.hasNextLine()) {
         
            //printing line by line
            System.out.println(scn.nextLine());
         }
      }
   }
}

Output

C:\java\javaprgstu\filehandling\File Handling Tutorial\Code\1_ReadFiles>javac Read2.java
C:\java\javaprgstu\filehandling\File Handling Tutorial\Code\1_ReadFiles>java Read2
Block1
This is an experimental file. Inserting the line 100
This is an experimental file. Inserting the line 101
This is an experimental file. Inserting the line 102
This is an experimental file. Inserting the line 103
This is an experimental file. Inserting the line 104
This is an experimental file. Inserting the line 105
This is an experimental file. Inserting the line 106
……………………………………

The Sample File used contains 3 Blocks of Lines with 177 lines in each block

Approach 3:- By using the FileReader.

Java Applications can use FileReader to read data from the text files. In this case, the content of the file is read as a stream of characters.

Algorithm

  • Step 1 − Set path of the file.

  • Step 2 − Read the file lines and the file is read as a characters.

  • Step 3 − Display the lines read from the file on the display/screen.

Explanation

The reader is the superclass of FileReader and the superclass of Reader class is the Object class. Specify the proper path of the file to read the large text.

Example (Approach 3)

import java.io.File;
import java.io.FileReader;
import java.io.IOException;
public class Read3{
   public static void main(String[] args){
      
      //Set the name of the file to read
      File fileone = new File("sample.txt");
      
      // make frd as a FileReader object
      try (FileReader frd = new FileReader(fileone)){
         int content1;
         while ((content1 = frd.read()) != -1) {
            System.out.print((char) content1);
         }
      } catch (IOException e) {
            e.printStackTrace();
         }
   }
}

Output

C:\java\javaprgstu\filehandling\File Handling Tutorial\Code\1_ReadFiles>javac Read3.java
C:\java\javaprgstu\filehandling\File Handling Tutorial\Code\1_ReadFiles>java Read3
Block1
This is an experimental file. Inserting the line 100
This is an experimental file. Inserting the line 101
This is an experimental file. Inserting the line 102
This is an experimental file. Inserting the line 103
This is an experimental file. Inserting the line 104
This is an experimental file. Inserting the line 105
This is an experimental file. Inserting the line 106
This is an experimental file. Inserting the line 107
……………………………………….

The Sample File used contains 3 Blocks of Lines with 177 lines in each block

Conclusion

In the above article, three different approaches are used to read the lines from a file using Java language. The first approach reads the lines as blocks of characters. Another approach reads the lines as a byte stream and the third approach reads the lines as characters.

Updated on: 23-Mar-2023

455 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements