Table of contents
1.
Introduction
2.
What is Byte Stream in Java?
2.1.
Benefits of Byte Stream  
2.2.
Example of Byte Stream in Java  
3.
What is Character Stream in Java?
3.1.
Benefits of Character Stream  
3.2.
Example of Character Stream in Java  
4.
Difference Between Byte Stream and Character Stream
5.
When to Use Character Stream over Byte Stream?
6.
When to Use Byte Stream over Character Stream?
7.
Frequently Asked Questions
7.1.
Can we use Byte Stream to read text files?
7.2.
Why do we need Character Streams when Byte Streams exist?
7.3.
Which is faster: Byte Stream or Character Stream?
8.
Conclusion
Last Updated: Mar 22, 2025
Medium

Character Stream and Byte Stream in Java

Author Gaurav Gandhi
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

In Java, streams are used to read and write data. There are two types of streams: Byte Stream and Character Stream. These streams help in handling data efficiently, whether it is from a file, network, or another source. 

In this article, we will discuss what byte streams and character streams are, how they work, their differences, and when to use each of them with examples.

What is Byte Stream in Java?

A Byte Stream in Java is used to handle input and output of raw binary data. It reads and writes data in bytes (8-bit data), making it suitable for handling audio, video, and image files. It uses InputStream and OutputStream classes to perform these operations.

Benefits of Byte Stream  

Byte Stream in Java is used to handle binary data. It reads & writes data in the form of bytes, which makes it ideal for working with files like images, audio, videos, or any non-text data. Let’s take a look at the some key benefits of using Byte Stream:  

1. Universal Data Handling: Byte Stream can process any type of data, whether it’s text, images, or audio. This makes it highly versatile.  
 

2. Efficiency: Since Byte Stream works directly with raw bytes, it’s faster & more efficient for handling large files or binary data.  
 

3. Low-Level Operations: It provides fine-grained control over data, allowing you to manipulate individual bytes.  

Example of Byte Stream in Java  

Let’s look at a simple example where we use Byte Stream to read & write data from a file. We’ll use `FileInputStream` to read bytes from a file & `FileOutputStream` to write bytes to a file.  

 

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;


public class ByteStreamExample {
    public static void main(String[] args) {
        // File paths
        String inputFile = "input.txt";
        String outputFile = "output.txt";


        // Using try-with-resources to automatically close streams
        try (FileInputStream fis = new FileInputStream(inputFile);
             FileOutputStream fos = new FileOutputStream(outputFile)) {


            int byteData;
            // Read bytes from input file & write to output file
            while ((byteData = fis.read()) != -1) {
                fos.write(byteData);
            }


            System.out.println("File copied successfully using Byte Stream!");


        } catch (IOException e) {
            System.out.println("An error occurred: " + e.getMessage());
        }
    }
}
You can also try this code with Online Java Compiler
Run Code


In this Code: 
1. FileInputStream: This class reads data from a file in the form of bytes. In the example, it reads from `input.txt`.  
 

2. FileOutputStream: This class writes data to a file in the form of bytes. In the example, it writes to `output.txt`.  
 

3. try-with-resources: This ensures that the streams are automatically closed after the operation, preventing resource leaks.  
 

4. fis.read(): This method reads one byte at a time from the file. It returns `-1` when the end of the file is reached.  
 

5. fos.write(): This method writes one byte at a time to the output file.  

What is Character Stream in Java?

A Character Stream in Java is used to handle input and output of character data. It reads and writes data in 16-bit Unicode characters, making it suitable for handling text files. It uses Reader and Writer classes for these operations.

Benefits of Character Stream  

Character Stream in Java is designed to handle text data. It reads & writes data in the form of characters, making it ideal for working with files like `.txt`, `.csv`, or any other text-based formats. Here are some key benefits of using Character Stream:  

1. Text-Specific Handling: Character Stream is optimized for text data. It automatically handles character encoding, making it easier to work with strings & characters.  
 

2. Readability: Since it deals with characters, the code becomes more readable & intuitive when working with text files.  
 

3. Supports Unicode: Character Stream supports Unicode, which means it can handle text in multiple languages & special characters.  

Example of Character Stream in Java  

Let’s look at a simple example where we use Character Stream to read & write text data from a file. We’ll use `FileReader` to read characters from a file & `FileWriter` to write characters to a file.  

import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
public class CharacterStreamExample {
    public static void main(String[] args) {
        // File paths
        String inputFile = "input.txt";
        String outputFile = "output.txt";

        // Using try-with-resources to automatically close streams
        try (FileReader fr = new FileReader(inputFile);
             FileWriter fw = new FileWriter(outputFile)) {

            int charData;
            // Read characters from input file & write to output file
            while ((charData = fr.read()) != -1) {
                fw.write(charData);
            }

            System.out.println("File copied successfully using Character Stream!");

        } catch (IOException e) {
            System.out.println("An error occurred: " + e.getMessage());
        }
    }
}
You can also try this code with Online Java Compiler
Run Code


In this Code  

1. FileReader: This class reads data from a file in the form of characters. In the example, it reads from `input.txt`.  
 

2. FileWriter: This class writes data to a file in the form of characters. In the example, it writes to `output.txt`.  
 

3. try-with-resources: This ensures that the streams are automatically closed after the operation, preventing resource leaks.  
 

4. fr.read(): This method reads one character at a time from the file. It returns `-1` when the end of the file is reached.  
 

5. fw.write(): This method writes one character at a time to the output file.  

Difference Between Byte Stream and Character Stream

FeatureByte StreamCharacter Stream
Data TypeWorks with bytes (8-bit)Works with characters (16-bit)
Classes UsedInputStream, OutputStreamReader, Writer
Suitable ForHandling binary files (images, audio, video)Handling text files
PerformanceFaster for binary dataMore efficient for text data
Encoding SupportNo support for encodingSupports different encodings

When to Use Character Stream over Byte Stream?

Use Character Stream when:

  • You are handling text-based files like .txt, .csv, .xml.
     
  • The data contains special characters or Unicode characters.
     
  • You need to support multiple languages.
     
  • Encoding and decoding of text is required.

When to Use Byte Stream over Character Stream?

Use Byte Stream when:

  • You are working with binary files like .jpg, .mp3, .mp4, .pdf.
     
  • The file does not contain text-based data.
     
  • You do not need encoding or decoding.
     
  • Performance is a priority, and you want to handle raw data.

Frequently Asked Questions

Can we use Byte Stream to read text files?

Yes, but it is not recommended as it does not handle character encoding properly. Character Stream is better for text files.

Why do we need Character Streams when Byte Streams exist?

Character Streams are specifically designed to handle text data efficiently, ensuring proper encoding and decoding, which Byte Streams do not support.

Which is faster: Byte Stream or Character Stream?

Byte Streams are faster for binary data, while Character Streams are optimized for text data.

Conclusion

In this article, we discussed Byte Stream and Character Stream in Java, their differences, and when to use each. Byte Streams are best for binary files, while Character Streams work well with text files. Using the right stream for the right task improves performance and ensures proper handling of data.

Live masterclass