How to Write a Program that Reads Text File Line by Line and Prints them on Console In Java

Before we move on to the code, first create a text file (input.txt) using notepad?




package javaapplication1;


import java.io.*;

public class JavaApplication1 {

   
    public static void main(String args[]) {
       

        FileReader fr = null;
        BufferedReader br = null; 
        
        try{
           
            fr = new FileReader("input.txt");
            br = new BufferedReader(fr);
            
            String line = br.readLine();
            
            while(line != null){
                System.out.println(line);
                line = br.readLine();
            }
            
            
        } catch(IOException ieox){
            System.out.println(ieox);
        } 
        
}
        
    }
    

Comments