Sobes.tech
Junior

What are the types of input-output streams in Java?

sobes.tech AI

Answer from AI

In Java, there are two main categories of input-output streams (java.io):

  1. Byte Streams: Work with raw bytes. Used for processing binary data (images, audio, serialized objects).

    • Main classes: InputStream (for reading) and OutputStream (for writing).
    • Derived classes for various data sources/destinations:
      • Files: FileInputStream, FileOutputStream
      • Byte arrays: ByteArrayInputStream, ByteArrayOutputStream
      • Buffering: BufferedInputStream, BufferedOutputStream
      • Filtering/Transformation: FilterInputStream, FilterOutputStream (parent for Buffered and others)
      • Objects (serialization): ObjectInputStream, ObjectOutputStream
      • Console input/output: System.in (InputStream), System.out (PrintStream extends OutputStream)
  2. Character Streams: Work with Unicode characters. Used for processing text data. Character streams often use byte streams under the hood, converting bytes to characters based on a specified encoding.

    • Main classes: Reader (for reading) and Writer (for writing).
    • Derived classes:
      • Files: FileReader, FileWriter
      • Character arrays: CharArrayReader, CharArrayWriter
      • Strings: StringReader, StringWriter
      • Buffering: BufferedReader, BufferedWriter
      • Filtering/Transformation: FilterReader, FilterWriter
      • Formatted input/output: PrintWriter (for writing), Scanner (for reading, but not a direct descendant of Reader, though often used for text input)
      • Console input/output: System.in (via InputStreamReader), System.out (via PrintWriter)

Summary table:

Category Read basis Write basis Works with Main application
Byte streams InputStream OutputStream Bytes Binary data, low-level input/output
Character streams Reader Writer Characters (char) Text data, working with encodings

Usage example:

// Reading from a file with a byte stream
import java.io.FileInputStream;
import java.io.IOException;

public class ByteStreamExample {
    public static void main(String[] args) {
        try (FileInputStream fis = new FileInputStream("data.bin")) {
            int data;
            while ((data = fis.read()) != -1) {
                // Process each byte
                System.out.println("Byte read: " + data);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
// Reading from a file with a character stream
import java.io.FileReader;
import java.io.IOException;

public class CharStreamExample {
    public static void main(String[] args) {
        try (FileReader fr = new FileReader("data.txt")) {
            int data;
            while ((data = fr.read()) != -1) {
                // Process each character
                System.out.print((char) data);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
What are the types of input-output streams in Java… - sobes.tech