Sobes.tech
Junior

What are the types of streams in Java?

sobes.tech AI

Answer from AI

In Java, there are different types of streams:

  1. Input Streams — for reading data (e.g., FileInputStream, BufferedInputStream).
  2. Output Streams — for writing data (e.g., FileOutputStream, BufferedOutputStream).
  3. Character Streams (Reader and Writer) — for working with text (e.g., FileReader, BufferedWriter).
  4. NIO Streams (java.nio.channels) — a more modern and efficient way to work with input/output, supporting non-blocking mode.
  5. Streams in the context of multithreading (Threads) — separate execution threads of the program, not directly related to I/O streams.

Example of using InputStream:

try (FileInputStream fis = new FileInputStream("file.txt")) {
    int data;
    while ((data = fis.read()) != -1) {
        System.out.print((char) data);
    }
} catch (IOException e) {
    e.printStackTrace();
}