Java Standard Library: Input/Output Streams
![Picture of the author](/_next/image?url=%2F_next%2Fstatic%2Fmedia%2Fauthor-profile-picture.8832aca3.jpg&w=1120&q=90)
- Published on
![Java Standard Library Input/Output Streams illustration](/_next/image?url=%2Fphotos%2Fblog-prototype.jpg&w=1120&q=90)
Introduction
Handling data, whether it's reading from or writing to a data source, is a pivotal aspect of programming. In Java, the Standard Library offers a potent set of tools to manage Input/Output (I/O) operations through various streams.
The Essence of Streams in Java
What is a Stream?
In Java, a stream represents a sequence of data. There are two main types of streams:
- InputStream: Used to read data from a source.
- OutputStream: Used to write data to a destination.
These streams facilitate the movement of data between a data source/sink (like a file or network socket) and a Java program.
Byte Streams and Character Streams
- Byte Streams: Handle I/O of bytes and are used to perform input and output of 8-bit bytes.
- Character Streams: Handle I/O of characters and use Unicode, therefore, can be used to read and write data in internationalized characters.
Exploring Java's I/O Stream Classes
FileInputStream and FileOutputStream
FileInputStream
and FileOutputStream
are used for reading from and writing to a file, respectively.
FileInputStream fis = new FileInputStream("input.txt");
FileOutputStream fos = new FileOutputStream("output.txt");
DataInputStream and DataOutputStream
These are used for handling primitive data types (like int
, float
) more easily.
DataInputStream dis = new DataInputStream(new FileInputStream("data.txt"));
DataOutputStream dos = new DataOutputStream(new FileOutputStream("data.txt"));
InputStreamReader and OutputStreamWriter
Used for converting byte streams to character streams.
InputStreamReader isr = new InputStreamReader(new FileInputStream("input.txt"));
OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream("output.txt"));
Reading and Writing with Streams
Reading from a File
try (FileInputStream fis = new FileInputStream("input.txt")) {
int i;
while ((i = fis.read()) != -1) {
System.out.print((char) i);
}
} catch (IOException e) {
e.printStackTrace();
}
Writing to a File
try (FileOutputStream fos = new FileOutputStream("output.txt")) {
String data = "Hello, Java I/O!";
byte[] dataArray = data.getBytes(); // Converting string into byte array
fos.write(dataArray);
} catch (IOException e) {
e.printStackTrace();
}
Buffering in Java I/O
Buffered streams (BufferedInputStream
, BufferedOutputStream
, BufferedReader
, and BufferedWriter
) are used to buffer data into a memory area, reducing the number of I/O operations.
try (BufferedWriter bw = new BufferedWriter(new FileWriter("output.txt"))) {
bw.write("Buffered I/O in Java");
} catch (IOException e) {
e.printStackTrace();
}
Conclusion
The Java Standard Library provides a comprehensive set of I/O stream classes to efficiently manage data flow in applications. Understanding these streams and how to manipulate them is crucial in handling file I/O, network communications, and inter-process communications effectively and efficiently.