Character Stream Vs Byte Stream in Java
Last Updated :
28 Aug, 2025
A stream is a continuous flow of data. In Java, I/O streams are used to read from input sources or write to output destinations, such as files. They provide a way to access data sequentially. The java.io package includes classes for handling both byte streams and character streams and also for converting between them.
- Input Stream: reads data from the source.
- Output Stream: writes data to a destination.
When to use Character Stream over Byte Stream:
In Java, characters are stored using Unicode conventions. Character stream is useful when we want to process text files. These text files can be processed character by character. Character size is typically 16 bits.
When to use Byte Stream over Character Stream:
Byte oriented reads byte by byte. A byte stream is suitable for processing raw data like binary files.
Key points while using and dealing with any of the above streams:
- Character stream classes usually end with Reader/Writer, while byte stream classes end with InputStream/OutputStream.
- The example codes in this article use unbuffered streams, which are less efficient. For better performance, we commonly wrap them with BufferedReader/BufferedWriter (character streams) or BufferedInputStream/BufferedOutputStream (byte streams).
- Always close streams after use to free resources and prevent errors.
- The above codes may not run in online compilers as files may not exist.
Character Stream
In Java, characters are stored using Unicode conventions. Character stream automatically allows us to read/write data character by character. For example, FileReader and FileWriter are character streams used to read from the source and write to the destination.
Character StreamsExample
Java
import java.io.*;
public class GFG {
public static void main(String[] args)
throws IOException
{
// Initially assigning null as we have not read anything
FileReader sourceStream = null;
try {
// Reading from file
sourceStream = new FileReader("/Users/mayanksolanki/Desktop/demo.rtf");
// Reading sourcefile and writing content to target file character by character.
int temp;
// If there is content inside file than read
while ((temp = sourceStream.read()) != -1)
System.out.println((char)temp);
System.out.print("Program successfully executed");
}
finally {
if (sourceStream != null)
sourceStream.close();
}
}
}
Output:
Writes content to the target file character by character
Program successfully executed
Byte Stream
A byte stream in Java is a stream that handles input and output of raw 8-bit binary data. It is mainly used for reading and writing non-text data such as images, audio, video or any binary file. For example, FileInputStream is used to read from the source and FileOutputStream to write to the destination.
Example:
Java
import java.io.*;
public class GFG {
public static void main(String[] args)
throws IOException
{
// Initially assigning null ot objects for reading and writing to file
FileInputStream sourceStream = null;
FileOutputStream targetStream = null;
try {
// Passing the files via local directory
sourceStream = new FileInputStream("/Users/mayanksolanki/Desktop/demo.rtf");
targetStream = new FileOutputStream("/Users/mayanksolanki/Desktop/democopy.rtf");
// Reading source file and writing content to file byte by byte
int temp;
// If there is content inside file than read
while ((temp = sourceStream.read()) != -1)
targetStream.write((byte)temp);
System.out.print("Program successfully executed");
}
// finally block that executes for sure where we are closing file connections to avoid memory leakage
finally {
if (sourceStream != null)
sourceStream.close();
if (targetStream != null)
targetStream.close();
}
}
}
Output:
Program successfully executed
Character Stream vs Byte Stream Differences
| Aspect | Character Stream | Byte Stream |
|---|
| Data type handled | 16-bit Unicode characters (text) | 8-bit raw data (binary) |
|---|
| Classes end with | Reader / Writer | InputStream / OutputStream |
|---|
| Suitable for | Text files, Unicode data | Images, audio, video, binary files |
|---|
| Conversion | Converts bytes to characters automatically | No conversion, works with raw bytes |
|---|
| Examples | FileReader, FileWriter | FileInputStream, FileOutputStream |
|---|
Related Article
Explore
Java Basics
OOP & Interfaces
Collections
Exception Handling
Java Advanced
Practice Java