Java Programming Handbook

    InputStreamReader and OutputStreamWriter Classes

    In Java, InputStreamReader and OutputStreamWriter are bridge classes that convert byte streams to character streams and vice versa. They are essential when you need to read or write text using specific character encodings.

    1. Class Hierarchy#

    java.lang.Object ┓ java.io.Reader ┓ java.io.InputStreamReader java.lang.Object ┓ java.io.Writer ┓ java.io.OutputStreamWriter
    • InputStreamReader extends Reader
    • OutputStreamWriter extends Writer

    2. InputStreamReader Class#

    ● Constructor#

    InputStreamReader(InputStream in) InputStreamReader(InputStream in, Charset charset) InputStreamReader(InputStream in, String charsetName)

    ● Example: Reading a file with InputStreamReader#

    import java.io.*; public class InputStreamReaderExample { public static void main(String[] args) { try (InputStreamReader reader = new InputStreamReader(new FileInputStream("input.txt"))) { int data; while ((data = reader.read()) != -1) { // Convert byte to char and print System.out.print((char) data); } } catch (IOException e) { e.printStackTrace(); } } }

    Output (If input.txt contains "Hello World"):#

    Hello World

    Explanation:#

    • Reads bytes from file, decodes them into characters using the platform’s default charset.
    • try-with-resources auto-closes the stream.

    int read()#

    Reads a single character.

    int read(char[] cbuf, int offset, int length)#

    Reads characters into an array with offset and length.

    import java.io.*; public class InputStreamReaderReadArray { public static void main(String[] args) { char[] buffer = new char[20]; try (InputStreamReader reader = new InputStreamReader(new FileInputStream("input.txt"))) { int charsRead = reader.read(buffer, 0, buffer.length); System.out.println("Read " + charsRead + " characters: " + new String(buffer, 0, charsRead)); } catch (IOException e) { e.printStackTrace(); } } }

    Output:#

    Read 11 characters: Hello World

    void close()#

    Closes the stream and releases any resources. If you're using try-with-resources, this is called automatically.

    import java.io.*; public class InputStreamReaderCloseExample { public static void main(String[] args) { InputStreamReader reader = null; //Note:- Try-with-resources ensures the stream is closed automatically try { reader = new InputStreamReader(new FileInputStream("input.txt")); System.out.println("Stream is open."); // You can perform operations here reader.close(); // Must be closed manually System.out.println("Stream is now closed."); } catch (IOException e) { e.printStackTrace(); } } }

    Output:#

    Stream is open. Stream is now closed.

    3. OutputStreamWriter Class#

    ● Constructor#

    OutputStreamWriter(OutputStream out) OutputStreamWriter(OutputStream out, Charset charset) OutputStreamWriter(OutputStream out, String charsetName)

    void write(int c) – Writes a single character#

    import java.io.*; public class OutputStreamWriterWriteChar { public static void main(String[] args) { try (OutputStreamWriter writer = new OutputStreamWriter(new FileOutputStream("output.txt"))) { writer.write(65); // Writes character 'A' to the output.txt file System.out.println("Character written."); } catch (IOException e) { e.printStackTrace(); } } }

    Output:#

    Character written.

    File content (output.txt):

    A

    Explanation:

    • The integer 65 is the ASCII value of character 'A'.
    • This method writes the corresponding character to the file.
    • Since we used try-with-resources, the stream is automatically closed.

    void write(char[] cbuf, int off, int len) – Writes characters from a char array#

    import java.io.*; public class OutputStreamWriterWriteArray { public static void main(String[] args) { char[] chars = "Hello Java".toCharArray(); try (OutputStreamWriter writer = new OutputStreamWriter(new FileOutputStream("output.txt"))) { writer.write(chars, 0, chars.length); System.out.println("Character array written."); } catch (IOException e) { e.printStackTrace(); } } }

    Output:#

    Character array written.

    File content (output.txt):

    Hello Java

    Explanation:

    • Converts the string "Hello Java" to a character array.
    • Writes the entire array to the file using the write(char[], off, len) method.
    • Here, off = 0 and len = chars.length writes the full content.

    void write(String str, int off, int len) – Writes a substring#

    import java.io.*; public class OutputStreamWriterWriteString { public static void main(String[] args) { String text = "OutputStreamWriter Example"; try (OutputStreamWriter writer = new OutputStreamWriter(new FileOutputStream("output.txt"))) { writer.write(text, 0, text.length()); System.out.println("String written to file."); } catch (IOException e) { e.printStackTrace(); } } }

    Output:#

    String written to file.

    File content (output.txt):

    OutputStreamWriter Example

    Explanation:

    • This example writes the full string to the file.
    • You can also write part of the string by changing the offset and length values.

    void flush() – Flushes the data forcibly to the file#

    import java.io.*; public class OutputStreamWriterFlushExample { public static void main(String[] args) { try (OutputStreamWriter writer = new OutputStreamWriter(new FileOutputStream("output.txt"))) { writer.write("Flush this data"); writer.flush(); // Forces the buffer to be written immediately System.out.println("Data flushed."); } catch (IOException e) { e.printStackTrace(); } } }

    Output:#

    Data flushed.

    File content (output.txt****):

    Flush this data

    Explanation:

    • flush() makes sure all characters in the internal buffer are written to disk.
    • Useful when you want to make sure data is saved before the stream is closed or reused.

    void close() – Closes the writer and releases resources#

    import java.io.*; public class OutputStreamWriterCloseExample { public static void main(String[] args) { OutputStreamWriter writer = null; try { writer = new OutputStreamWriter(new FileOutputStream("output.txt")); writer.write("Closing Example"); writer.close(); // Manually closing System.out.println("Stream closed successfully."); } catch (IOException e) { e.printStackTrace(); } } }

    Output:#

    Stream closed successfully.

    File content (output.txt****):

    Closing Example

    Explanation:

    • Explicitly closes the writer using close().
    • With try-with-resources (as used in other examples), calling close() manually isn’t required—it happens automatically.

    Conclusion#

    In this blog, we covered InputStreamReader and OutputStreamWriter classes, which act as bridges between byte streams and character streams.

    Next, we’ll dive into FileReader and FileWriter classes, which are designed specifically for reading and writing character files.

    Last updated on Apr 09, 2025