What is the Difference Between Byte Stream and Character Stream in Java

The main difference between Byte Stream and Character Stream in Java is that the Byte Stream helps to perform input and output operations of 8-bit bytes while the Character Stream helps to perform input and output operations of 16-bit Unicode.

A stream is a sequence of data that is available over time. A source generates data as a stream. Destination consumes or read data are available as a stream. In other words, a stream explains the flow of data which allows reading or writing. Two ways of performing operations on streams in Java are by using Byte Stream and character stream.

Key Areas Covered

1. What is Byte Stream in Java
     – Definition, Functionality
2. What is Character Stream in Java
     – Definition, Functionality
3. What is the Difference Between Byte Stream and Character Stream in Java
     – Comparison of Key Differences

Key Terms

Byte Stream, Character Stream, Java

Difference Between Byte Stream and Character Stream in Java - Comparison Summary

What is Byte Stream in Java

Byte streams in Java help to perform input and output operations of 8-bit bytes. In other words, it processes data byte by byte. The most frequently used classes for Byte stream operations are FileInputStream and FileOutputStream. The FileInputStream helps to read from the source while FileOutputStream helps to write to the destination.

Difference Between Byte Stream and Character Stream in Java

import java.io.*;

public class Program {

 

   public static void main(String args[]) throws IOException {        

      FileInputStream in = null;

      FileOutputStream out = null;

 

      try {

         in = new FileInputStream(“input.txt”);

         out = new FileOutputStream(“output.txt”);

         int c;

         while ((c = in.read()) != -1) {

            out.write(c);

         }

      }finally {

         if (in != null) {

            in.close();

         }

         if (out != null) {

            out.close();

         }

      }

   }

}

According to the above program, there are two objects of FileInputStream and FileOutputStream. The while loop reads data in the input.txt file and writes them in the new file output.txt until reaching the end of the file. The finally block will close the files. Finally, the output.txt file will also have the same content as the input.txt file. Usually, it is possible to use Byte Stream with any file type.

What is Character Stream in Java

Character stream in Java helps to perform input and output for 16 bit Unicode.  The most common classes for character streaming in Java are FileReader and FileWriter. Internally, FileReader uses FileInputStream. Similarly, the FileWrite uses FileOutputStream.

import java.io.*;

public class Program {

 

   public static void main(String args[]) throws IOException {

      FileReader in = null;

      FileWriter out = null;

 

      try {

         in = new FileReader(“input.txt”);

         out = new FileWriter(“output.txt”);

         int c;

         while ((c = in.read()) != 1) {

            out.write(c);

         }

      }finally {

         if (in != null) {

            in.close();

         }

         if (out != null) {

            out.close();

         }

      }

   }

According to the above program, there are two objects of FileReader and FileWriter. The while loop reads the Unicode characters in the input txt file and writes them to the new file called output.txt until reaching the end of the file. The finally block will close the files. In the end, the output.txt file will also have the same content as the input.txt file. The FileReader reads two bytes at a time while FileWriter writes two bytes at a time.

Difference Between Byte Stream and Character Stream in Java

Definition

Byte Stream is a mechanism that performs input and output of 8-bit bytes while Character Stream is a mechanism in Java that performs input and output operations of 16-bit Unicode. Thus, this is the main difference between Byte Stream and Character Stream in Java.

Functionality

Another difference between Byte Stream and Character Stream in Java is that Byte Stream performs input and output operations of 8-bit bytes while Character stream performs input and output operations of 16-bit Unicode.

Associated Classes

The common classes for byte streaming in Java are FileInputStream and FileOutputStream. However, the common classes for Character streaming in Java are FileReader and FileWriter. Hence, this is another difference between Byte Stream and Character Stream in Java.

Conclusion

A stream refers to a sequence of data. Two methods of performing operations on streams in Java are using Byte Stream and character stream. The main difference between Byte Stream and Character Stream in Java is that Byte Stream helps to perform input and output operations of 8-bit bytes while Character stream helps to perform input and output operations of 16-bit Unicode.

Reference:

1. “Byte Streams.” What Every Computer Scientist Should Know About Floating-Point Arithmetic, Available here.
2. “Character Streams.” What Every Computer Scientist Should Know About Floating-Point Arithmetic, Available here.

Image Courtesy:

1. “147386” (CC0) via Pixabay

About the Author: Lithmee

Lithmee holds a Bachelor of Science degree in Computer Systems Engineering and is reading for her Master’s degree in Computer Science. She is passionate about sharing her knowldge in the areas of programming, data science, and computer systems.

Leave a Reply