Junior
What are the types of streams in Java?
sobes.tech AI
Answer from AI
In Java, there are different types of streams:
- Input Streams — for reading data (e.g., FileInputStream, BufferedInputStream).
- Output Streams — for writing data (e.g., FileOutputStream, BufferedOutputStream).
- Character Streams (Reader and Writer) — for working with text (e.g., FileReader, BufferedWriter).
- NIO Streams (java.nio.channels) — a more modern and efficient way to work with input/output, supporting non-blocking mode.
- 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();
}