Java-BufferedReader

BufferedReader

The BufferedReader class possesses the functionality of reading buffers of characters from a file. In the following program, an object of FileReader class is first created and associated with the data file output.txt. Then, an object of BufferedReader class is created. Subsequently, the names of five national parks, which have been written into the data file output.txt in previous program, are retrieved, one by one, by invoking the readLine( ) method of the BufferedReader class, and then displayed. Finally, both the file streams are closed.

Program

final

Program Source

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

class Javaapp {
    
    public static void main(String[] args) {
        
        FileReader fr = null;
        BufferedReader br = null;
        
        try{
            fr = new FileReader("output.txt");
            br = new BufferedReader(fr);
            String getline;
            while((getline=br.readLine())!=null)
            {
                System.out.println(getline);
            }
        }catch(IOException io)
        {
            System.out.println("Exception : "+io);
            
        }finally{
            
            try{
                br.close();
                fr.close();
            }catch(IOException io)
            {
                System.out.println("Exception : "+io);
            }
        }
    }
}

Leave a Comment