Java Tutorial

Operators

Flow Control

String

Number and Date

Built-in Classes

Array

Class and Object

Inheritance and Polymorphism

Exception Handling

Collections, Generics and Enumerations

Reflection

Input/Output Stream

Annotation

Java InputStreamReader And OutputStreamWriter

In Java, InputStreamReader and OutputStreamWriter are character stream classes that facilitate reading and writing text from/to byte streams. They allow you to convert bytes to characters (and vice versa) using specified character encodings. In this tutorial, we will cover the basics of using InputStreamReader and OutputStreamWriter in Java.

  • Using InputStreamReader

InputStreamReader is a subclass of Reader and can be used to read characters from an InputStream. It reads bytes from the input stream and converts them into characters using a specified character encoding or the default encoding of the system.

Example:

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

public class InputStreamReaderExample {
    public static void main(String[] args) {
        try {
            FileInputStream fis = new FileInputStream("input.txt");
            InputStreamReader isr = new InputStreamReader(fis, "UTF-8");

            int c;
            while ((c = isr.read()) != -1) {
                System.out.print((char) c);
            }

            isr.close();
            fis.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
  • Using OutputStreamWriter

OutputStreamWriter is a subclass of Writer and can be used to write characters to an OutputStream. It converts characters into bytes using a specified character encoding or the default encoding of the system.

Example:

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;

public class OutputStreamWriterExample {
    public static void main(String[] args) {
        try {
            FileOutputStream fos = new FileOutputStream("output.txt");
            OutputStreamWriter osw = new OutputStreamWriter(fos, "UTF-8");

            String text = "Hello, World!";
            osw.write(text);

            osw.close();
            fos.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
  • Reading and Writing Files with InputStreamReader and OutputStreamWriter

Here's an example of reading from a file using InputStreamReader and writing to another file using OutputStreamWriter:

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;

public class InputStreamReaderAndOutputStreamWriterExample {
    public static void main(String[] args) {
        try {
            // Reading from a file
            FileInputStream fis = new FileInputStream("input.txt");
            InputStreamReader isr = new InputStreamReader(fis, "UTF-8");

            // Writing to a file
            FileOutputStream fos = new FileOutputStream("output.txt");
            OutputStreamWriter osw = new OutputStreamWriter(fos, "UTF-8");

            int c;
            while ((c = isr.read()) != -1) {
                osw.write(c);
            }

            // Closing resources
            isr.close();
            fis.close();
            osw.close();
            fos.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

In summary, InputStreamReader and OutputStreamWriter are character stream classes in Java that can be used to read and write text from/to byte streams. They allow you to convert bytes to characters (and vice versa) using specified character encodings, making it easier to work with text in different languages and character sets.

  1. Reading and writing text files with InputStreamReader and OutputStreamWriter

    InputStreamReader and OutputStreamWriter are used for reading and writing character-based data respectively. They bridge byte streams to character streams.

    try (FileInputStream fis = new FileInputStream("input.txt");
         FileOutputStream fos = new FileOutputStream("output.txt");
         InputStreamReader isr = new InputStreamReader(fis);
         OutputStreamWriter osw = new OutputStreamWriter(fos)) {
    
        int data;
        while ((data = isr.read()) != -1) {
            osw.write(data);
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
    
  2. Character encoding with InputStreamReader and OutputStreamWriter in Java

    You can specify a character encoding when creating InputStreamReader and OutputStreamWriter. For example, using UTF-8:

    InputStreamReader isr = new InputStreamReader(new FileInputStream("input.txt"), StandardCharsets.UTF_8);
    OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream("output.txt"), StandardCharsets.UTF_8);
    
  3. Using InputStreamReader and OutputStreamWriter with FileInputStream and FileOutputStream

    InputStreamReader and OutputStreamWriter can be paired with FileInputStream and FileOutputStream to read from and write to files.

    InputStreamReader isr = new InputStreamReader(new FileInputStream("input.txt"));
    OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream("output.txt"));
    
  4. BufferedInputStream and BufferedOutputStream with InputStreamReader and OutputStreamWriter

    Combining BufferedInputStream and BufferedOutputStream with InputStreamReader and OutputStreamWriter can improve performance when dealing with large amounts of data.

    try (BufferedInputStream bis = new BufferedInputStream(new FileInputStream("input.txt"));
         BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("output.txt"));
         InputStreamReader isr = new InputStreamReader(bis);
         OutputStreamWriter osw = new OutputStreamWriter(bos)) {
    
        int data;
        while ((data = isr.read()) != -1) {
            osw.write(data);
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
    
  5. Java InputStreamReader vs. FileReader differences

    InputStreamReader is more versatile as it allows specifying a character encoding. FileReader uses the platform's default character encoding, which might not be suitable for all scenarios.

    // Using InputStreamReader
    InputStreamReader isr = new InputStreamReader(new FileInputStream("input.txt"), StandardCharsets.UTF_8);
    
    // Using FileReader
    FileReader fr = new FileReader("input.txt"); // Uses platform's default encoding
    
  6. Handling different character encodings in Java streams

    You can handle different character encodings by specifying the desired encoding when creating InputStreamReader and OutputStreamWriter.

    InputStreamReader isr = new InputStreamReader(new FileInputStream("input.txt"), StandardCharsets.UTF_16);
    OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream("output.txt"), StandardCharsets.UTF_16);
    
  7. UTF-8 and UTF-16 encoding in InputStreamReader and OutputStreamWriter

    Example using UTF-8 encoding:

    InputStreamReader isr = new InputStreamReader(new FileInputStream("input.txt"), StandardCharsets.UTF_8);
    OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream("output.txt"), StandardCharsets.UTF_8);
    

    Example using UTF-16 encoding:

    InputStreamReader isr = new InputStreamReader(new FileInputStream("input.txt"), StandardCharsets.UTF_16);
    OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream("output.txt"), StandardCharsets.UTF_16);
    
  8. Converting byte streams to character streams with InputStreamReader and OutputStreamWriter

    InputStreamReader converts an InputStream (byte stream) to a character stream, and OutputStreamWriter converts an OutputStream (byte stream) to a character stream.

    // Converting byte stream to character stream
    InputStreamReader isr = new InputStreamReader(new FileInputStream("input.txt"), StandardCharsets.UTF_8);
    
    // Converting byte stream to character stream
    OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream("output.txt"), StandardCharsets.UTF_8);