Explain InputStream class, OutputStream class, Reader class and Writer class

Explain InputStream class, OutputStream class, Reader class and Writer class

Explain InputStream class, OutputStream class, Reader class and Writer class with
suitable java code.

1. InputStream Class

The InputStream class is an abstract class that represents input byte streams. It is the superclass of all byte input stream classes. The methods in this class are used to read bytes from an underlying input source.

Key Methods:

  • int read(): Reads the next byte of data from the input stream.
  • int read(byte[] b): Reads up to b.length bytes of data into an array.
  • void close(): Closes the input stream and releases any system resources associated with it.

Example:

import java.io.*;

public class InputStreamExample {
    public static void main(String[] args) throws IOException {
        FileInputStream inputStream = new FileInputStream("example.txt");
        int data;
        while ((data = inputStream.read()) != -1) {
            System.out.print((char) data);
        }
        inputStream.close();
    }
}

2. OutputStream Class

The OutputStream class is an abstract class that represents output byte streams. It is the superclass of all byte output stream classes. The methods in this class are used to write bytes to an output destination, like a file or network.

Key Methods:

  • void write(int b): Writes the specified byte to the output stream.
  • void write(byte[] b): Writes b.length bytes from the specified byte array to the output stream.
  • void close(): Closes the output stream and releases any system resources associated with it.

Example:

import java.io.*;

public class OutputStreamExample {
    public static void main(String[] args) throws IOException {
        FileOutputStream outputStream = new FileOutputStream("output.txt");
        String text = "Hello, World!";
        outputStream.write(text.getBytes());
        outputStream.close();
    }
}

3. Reader Class

The Reader class is an abstract class that represents input character streams. It is used to read characters, arrays, and lines of text. Unlike InputStream, which deals with bytes, Reader works with character data.

Key Methods:

  • int read(): Reads a single character from the stream.
  • int read(char[] c): Reads characters into an array.
  • void close(): Closes the stream and releases any system resources associated with it.

Example:

import java.io.*;

public class ReaderExample {
    public static void main(String[] args) throws IOException {
        FileReader reader = new FileReader("example.txt");
        int data;
        while ((data = reader.read()) != -1) {
            System.out.print((char) data);
        }
        reader.close();
    }
}

4. Writer Class

The Writer class is an abstract class that represents output character streams. It is used to write characters, arrays, and lines of text to an output destination.

Key Methods:

  • void write(int c): Writes a single character to the stream.
  • void write(char[] c): Writes characters from an array to the stream.
  • void close(): Closes the stream and releases any system resources associated with it.

Example:

import java.io.*;

public class WriterExample {
    public static void main(String[] args) throws IOException {
        FileWriter writer = new FileWriter("output.txt");
        String text = "Hello, Writer!";
        writer.write(text);
        writer.close();
    }
}