Reading a Text file in java

Java.IO PackageJava Programming Java8

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
raja
Published on 04-Jun-2018 12:34:08
Advertisements