Reading a Text file in java


Java provides Reader classes to read data from various sources. You can read the contents of a file using the BufferedReader class.

Program

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

public class BufferedReaderExample {
   public static void main(String[] args) {
      try (BufferedReader br = new BufferedReader(new FileReader("myFile"))) {
         String sCurrentLine;
         
         while ((sCurrentLine = br.readLine()) != null) {
            System.out.println(sCurrentLine);
         }
      } catch (IOException e) {
         e.printStackTrace();
      }
   }
}

Output

Hi welcome to Tutorialspoint

Updated on: 25-Jun-2020

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements