
- Java.io - Home
- Java.io - BufferedInputStream
- Java.io - BufferedOutputStream
- Java.io - BufferedReader
- Java.io - BufferedWriter
- Java.io - ByteArrayInputStream
- Java.io - ByteArrayOutputStream
- Java.io - CharArrayReader
- Java.io - CharArrayWriter
- Java.io - Console
- Java.io - DataInputStream
- Java.io - DataOutputStream
- Java.io - File
- Java.io - FileDescriptor
- Java.io - FileInputStream
- Java.io - FileOutputStream
- Java.io - FilePermission
- Java.io - FileReader
- Java.io - FileWriter
- Java.io - FilterInputStream
- Java.io - FilterOutputStream
- Java.io - FilterReader
- Java.io - FilterWriter
- Java.io - InputStream
- Java.io - InputStreamReader
- Java.io - LineNumberInputStream
- Java.io - LineNumberReader
- Java.io - ObjectInputStream
- Java.io - ObjectInputStream.GetField
- Java.io - ObjectOutputStream
- io - ObjectOutputStream.PutField
- Java.io - ObjectStreamClass
- Java.io - ObjectStreamField
- Java.io - OutputStream
- Java.io - OutputStreamWriter
- Java.io - PipedInputStream
- Java.io - PipedOutputStream
- Java.io - PipedReader
- Java.io - PipedWriter
- Java.io - PrintStream
- Java.io - PrintWriter
- Java.io - PushbackInputStream
- Java.io - PushbackReader
- Java.io - RandomAccessFile
- Java.io - Reader
- Java.io - SequenceInputStream
- Java.io - SerializablePermission
- Java.io - StreamTokenizer
- Java.io - StringBufferInputStream
- Java.io - StringReader
- Java.io - StringWriter
- Java.io - Writer
- Java.io package Useful Resources
- Java.io - Discussion
Java - InputStreamReader close() method
Description
The Java InputStreamReader close() method is used to close the stream and release system resources associated with it. No more reading can be done after closing.
Declaration
Following is the declaration for java.io.InputStreamReader.close() method −
public void close()
Parameters
NA
Return Value
The method does not return any value.
Exception
IOException − If an I/O error occurs.
Example - Usage of InputStreamReader close() method
The following example shows the usage of Java InputStreamReader close() method.
InputStreamReaderDemo.java
package com.tutorialspoint; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStreamReader; public class InputStreamReaderDemo { public static void main(String[] args) throws IOException { FileInputStream fis = null; InputStreamReader isr = null; int i; char c; try { // new input stream reader is created fis = new FileInputStream("test.txt"); isr = new InputStreamReader(fis); // input stream reader is closed isr.close(); System.out.println("close() invoked"); // read() called after closed method i = isr.read(); c = (char)i; System.out.println(c); } catch (Exception e) { // print error System.out.println("The stream is already closed"); } finally { // closes the stream and releases resources associated if(fis!=null) fis.close(); if(isr!=null) isr.close(); } } }
Output(Assuming test.txt contains "ABCDE")
Let us compile and run the above program, this will produce the following result−
close() invoked The stream is already closed
Example - Using close() with InputStreamReader (Automatic Closure with Try-With-Resources)
The following example shows the usage of Java InputStreamReader close() method.
InputStreamReaderDemo.java
package com.tutorialspoint; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStreamReader; public class InputStreamReaderDemo { public static void main(String[] args) { try (InputStreamReader reader = new InputStreamReader(new FileInputStream("example.txt"))) { int data; while ((data = reader.read()) != -1) { // Read character by character System.out.print((char) data); } // No need to manually close, try-with-resources handles it } catch (IOException e) { e.printStackTrace(); } } }
Output(if example.txt contains "JavaProgramming")
Let us compile and run the above program, this will produce the following result−
JavaProgramming
Explanation
Uses InputStreamReader, which reads characters from a file.
Reads one character at a time using read().
Uses try-with-resources, which automatically calls close() at the end.
Example - Manually Closing InputStreamReader in a finally Block
The following example shows the usage of Java InputStreamReader close() method.
InputStreamReaderDemo.java
package com.tutorialspoint; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStreamReader; public class InputStreamReaderDemo { public static void main(String[] args) { InputStreamReader reader = null; try { reader = new InputStreamReader(new FileInputStream("example.txt")); int data = reader.read(); System.out.println("First character read: " + (char) data); } catch (IOException e) { e.printStackTrace(); } finally { try { if (reader != null) { reader.close(); // Manually closing the reader System.out.println("Stream closed successfully."); } } catch (IOException e) { e.printStackTrace(); } } } }
Output(if example.txt contains "Java")
Let us compile and run the above program, this will produce the following result−
First character read: J Stream closed successfully.
Explanation
Uses InputStreamReader with FileInputStream to read a file.
Reads the first character and prints it.
Manually closes the reader inside finally to ensure resources are freed.
Prevents memory leaks by checking if (reader != null) before calling close().