Java.io.BufferedReader.Close() Method

Advertisements


Description

The java.io.BufferedInputStream.Close() method closes the stream and releases any system resources associated with it.

After the closing of the stream, read(), ready(), mark(), reset(), or skip() invocation will throw IOException.

Declaration

Following is the declaration for java.io.BufferedReader.close() method

public void close()

Parameters

  • NA

Return Value

This method does not return any value.

Exception

  • IOException -- -- if an I/O error occurs..

Example

The following example shows the usage of java.io.BufferedReader.close() method.

package com.tutorialspoint;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

public class BufferedReaderDemo {
   public static void main(String[] args) throws Exception {
      
      InputStream is = null; 
      InputStreamReader isr = null;
      BufferedReader br = null;

      try{
         // open input stream test.txt for reading purpose.
         is = new FileInputStream("c:/test.txt");
         
         // create new input stream reader
         isr = new InputStreamReader(is);
         
         // create new buffered reader
         br = new BufferedReader(isr);
         
         // releases any system resources associated with reader
         br.close();
         
         // creates error
         br.read();
         
      }catch(IOException e){
         
         // IO error
         System.out.println("The buffered reader is closed");
      }finally{
         
         // releases any system resources associated
         if(is!=null)
            is.close();
         if(isr!=null)
            isr.close();
         if(br!=null)
            br.close();
      }
   }
}

Assuming we have a text file c:/test.txt, which has the following content. This file will be used as an input for our example program:

ABCDEFGHIJKLMNOPQRSTUVWXYZ

Let us compile and run the above program, this will produce the following result:

IOException: The buffered reader is closed



Advertisements
Advertisements