Java - Character Read from File

Introduction

Create a BufferedReader object by wrapping a FileReader object and read one line of text at a time using its readLine() method.

The readLine() method considers a linefeed ( '\n'), a carriage return ( '\r'), and a carriage return followed by a linefeed as a line terminator.

It returns the text of the line excluding the line terminator.

It returns null when the end of the stream is reached.

The following code reads the text from the test.txt file.

Demo

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

public class Main {
  public static void main(String[] args) throws Exception{
    String srcFile = "test.txt";
    BufferedReader br = new BufferedReader(new FileReader(srcFile));
    String text = null;/*  ww w.j a  v  a2s  .c o m*/

    while ((text = br.readLine()) != null) {
      System.out.println(text);
    }

    br.close();

  }
}

Result