Java Program to Read the Content of a File Line by Line

To understand this example, you should have the knowledge of the following Java programming topics:


Example 1: Java Program to Read File Using BufferedInputStream

import java.io.BufferedInputStream;
import java.io.FileInputStream;

class Main {
  public static void main(String[] args) {
    try {

      // Creates a FileInputStream
      FileInputStream file = new FileInputStream("input.txt");

      // Creates a BufferedInputStream
      BufferedInputStream input = new BufferedInputStream(file);

      // Reads first byte from file
      int i = input .read();

      while (i != -1) {
        System.out.print((char) i);

        // Reads next byte from the file
        i = input.read();
      }
      input.close();
    }

    catch (Exception e) {
      e.getStackTrace();
    }
  }
}

Output

First Line
Second Line
Third Line
Fourth Line
Fifth Line

In the above example, we have used the BufferedInputStream Class to read each line from the file named input.txt.

Note: In order to run this file, you should have a file named input.txt in your current working directory.


Example 2: Java Program to Read File Using BufferedReader

import java.io.FileReader;
import java.io.BufferedReader;

class Main {
  public static void main(String[] args) {

    // Creates an array of character
    char[] array = new char[100];

    try {
      // Creates a FileReader
      FileReader file = new FileReader("input.txt");

      // Creates a BufferedReader
      BufferedReader input = new BufferedReader(file);

      // Reads characters
      input.read(array);
      System.out.println("Data in the file: ");
      System.out.println(array);

      // Closes the reader
      input.close();
    }

    catch(Exception e) {
      e.getStackTrace();
    }
  }
}

Output

Data in the file: 
First Line
Second Line
Third Line
Fourth Line
Fifth Line

In the above example, we have used the BufferedReader Class to read the file named input.txt.


Example 3: Java Program to Read File Using Scanner

import java.io.File;
import java.util.Scanner;

class Main {
  public static void main(String[] args) {

    try {
      // create a new file object
      File file = new File("input.txt");

      // create an object of Scanner
      // associated with the file
      Scanner sc = new Scanner(file);

      // read each line from file and print it
      System.out.println("Reading File Using Scanner:");
      while(sc.hasNextLine()) {
        System.out.println(sc.nextLine());
      }

      // close scanner
      sc.close();
    } catch (Exception e) {
      e.getStackTrace();
    }
  }
}

Output

Reading File Using Scanner:
First Line
Second Line
Third Line
Fourth Line
Fifth Line

In the above example, we have created an object of File class named file. We then created a Scanner object associated with the file.

Here, we have used the scanner methods

  • hasNextLine() - returns true if there is next line in the file
  • nextLine() - returns the entire line from the file

To learn more on scanner, visit Java Scanner.

Did you find this article helpful?