In computer science, a stream is a sequence of data. In Java, an I/O Stream refers to a stream that is not necessarily a method to sequentially access a file. Instead, an I/O Stream represents an input source or output destination, which can be different types of sources such as disk files. The java.io package provides classes that allow you to convert between Unicode character streams and byte streams of non-Unicode text. In this article, we will explore the differences between Character Streams and Byte Streams, and when to use them.
Character Stream In Java, characters are stored using Unicode conventions. A Character stream is useful when we want to process text files. These text files can be processed character by character. The character size is typically 16 bits. FileReader and FileWriter are character streams used to read from the source and write to the destination.
Example:
javaimport java.io.*;
public class CharacterStreamExample {
public static void main(String[] args) throws IOException {
FileReader fileReader = null;
try {
fileReader = new FileReader("/path/to/file.txt");
int character;
while ((character = fileReader.read()) != -1) {
System.out.print((char) character);
}
System.out.println("\nProgram successfully executed.");
} finally {
if (fileReader != null) {
fileReader.close();
}
}
}
}
Key points to keep in mind while using and dealing with Character Streams are:
- The names of character streams typically end with Reader/Writer.
- It is always recommended to close the stream if it is no longer in use to avoid any memory leakage.
- Buffered readers/writers should be used for efficiency instead of unbuffered streams.
Byte Stream Byte streams process data byte by byte (8 bits). A Byte stream is suitable for processing raw data like binary files. FileInputStream is used to read from the source, and FileOutputStream to write to the destination.
Example:
javaimport java.io.*;
public class ByteStreamExample {
public static void main(String[] args) throws IOException {
FileInputStream fileInputStream = null;
FileOutputStream fileOutputStream = null;
try {
fileInputStream = new FileInputStream("/path/to/input/file.bin");
fileOutputStream = new FileOutputStream("/path/to/output/file.bin");
int byteData;
while ((byteData = fileInputStream.read()) != -1) {
fileOutputStream.write((byte) byteData);
}
System.out.println("\nProgram successfully executed.");
} finally {
if (fileInputStream != null) {
fileInputStream.close();
}
if (fileOutputStream != null) {
fileOutputStream.close();
}
}
}
}
Key points to keep in mind while using and dealing with Byte Streams are:
- The names of byte streams typically end with InputStream/OutputStream.
- It is always recommended to close the stream if it is no longer in use to avoid any memory leakage.
- Buffered input streams/output streams should be used for efficiency instead of unbuffered streams.
Conclusion In Java, both Character Streams and Byte Streams are used to process data from different types of sources. When processing text files, Character Streams should be used, while Byte Streams should be used to process raw data like binary files. It is always recommended to close the stream if it is no longer in use to avoid any memory leakage. Buffered readers/writers and Buffered input streams/output streams should be used for efficiency instead of unbuffered streams.