Java IO Tutorial - Java Reader.close()








Syntax

Reader.close() has the following syntax.

public abstract void close()   throws IOException

Example

In the following code shows how to use Reader.close() method.

//from   w  w  w .j a v a2  s .c o  m

import java.io.*;

public class Main {

   public static void main(String[] args) {

      String s = "tutorial from java2s.com";

      Reader reader = new StringReader(s);

      try {
         // read the first five chars
         for (int i = 0; i < 5; i++) {
            char c = (char) reader.read();
            System.out.println(c);
         }
         reader.close();
      } catch (IOException ex) {
         ex.printStackTrace();
      }
   }
}

The code above generates the following result.