Character and Byte Streams in Java : Differences

In this tutorial, we will be looking up for the wide variations between character stream and byte stream in Java. We know that stream is a sequence of data stored in a file.  Java i/o streams are such that the input stream reads the data from a source and the output stream delivers the same data to the required target. Therefore, we can do this via both character streams and byte streams. We can use the Java package, java.io package because java.io package has the i/o streams which are the character stream and byte stream. Moreover, we use the java.io package because it helps to copy the useful content from a source file to a target file. The source file is the input file while the target file is the output file.

Let’s look at this in a comparative nature. Here, we will discuss the main differences between Character and Byte Streams. We will also have a detailed description of the two streams.

Analysis of variations between “CHARACTER STREAM” and “BYTE STREAM” in Java based on some parameters

  • Also Referred To As:-

Character stream is referred to as Reader and Writer streams. However, Byte stream is referred to as InputStream and OutputStream.

  • Type Of Access:-

Character stream can access a file character by character. However, Byte stream can access a file byte by byte.

  • Type Of Data Stored:-

Character stream reads and writes the text files. However, Byte stream can store image files, audio files, video files, etc.

  • Type Of Data On Which The Operation Needs To Be Performed:-

Character stream is used to perform operations on 16-bit UNICODE data. However, Byte stream is used to perform operations on 8-bit bytes data.

  • Classes Generally Used:-

Character stream uses FileReader and FileWriter. However,  Byte stream uses FileInputStream and FileOutputStream.

  • Encoding Required:-

Character stream uses encoding character sets like ASCII, Unicode, UTF-8, UTF-16 etc. However, Byte stream does not use any encoding functionalities.

  • Data Type Specificity:-

Character stream can read string type or character type files only. However, Byte stream is not a datatype specific for reading the files.

Sample Code:-

Below is the implementation of the concept of Character and Byte Stream in Java Programming Language.

Character stream:

//code to demonstrate the Character Stream of copying contents of source file to destination file. 
//importing java.io package
import java.io.*;
public class codeForCharacterStream {

public static void main(String args[]) throws IOException {
//declaring the input and output files  
      FileReader input_file = null;
      FileWriter output_file = null;

  try {
         input_file = new 				FileReader("input_file_for_character_stream.txt");
         output_file = new FileWriter("output_file_for_character_stream.txt");
//Reading source file and writing content to destination file byte by byte 
         int copy_file_data;
         while ((copy_file_data = input_file.read()) != -1) {
            output_file.write(copy_file_data);
         }
      }
  finally {
         if (input_file != null) {
            input_file.close();
         }
         if (output_file != null) {
            output_file.close();
         }
      }
   }
}

Byte stream:

//code to demonstrate the Byte Stream of copying contents of source file to destination file. 
//importing java.io package
import java.io.*;
public class codeForByteStream {

public static void main(String args[]) throws IOException { 
//declaring the input and output files       
      FileInputStream input_file = null;
      FileOutputStream output_file = null;

  try {
         input_file = new FileInputStream("input_file_for_byte_stream.txt");
         output_file = new FileOutputStream("output_file_for_byte_stream.txt");
//Reading source file and writing content to destination file byte by byte 
         int copy_file_data;
         while ((copy_file_data = input_file.read()) != -1) {
            output_file.write(copy_file_data);
         }
      }
  finally {
         if (input_file != null) {
            input_file.close();
         }
         if (output_file != null) {
            output_file.close();
         }
      }
   }
}

Output:-

Before writing the sample code, we need to write an input file. We do this because we want to copy this input file data to another file as our output file. So, we do the following as given below.

Character stream:

Firstly, we write a text file named, for example, “input_file_for_character_stream.txt” having any content, for instance, “WELCOME TO CODESPEEDY! LEARN CHARACTER STREAM!”.

So, we write the sample code as above to copy the source file “input_file_for_character_stream.txt” to destination file named, for example, “output_file_for_character_stream.txt”. After that, we need to compile and execute this sample code.

// To compile the sample code

$ javac codeForCharacterStream.java 

// To execute the sample code

$ java codeForCharacterStream 

Hence on execution, the text file  “output_file_for_character_stream.txt” is obtained.  Therefore, it has the content of the source file “input_file_for_character_stream.txt”. So, it is copied successfully using character stream in Java.

WELCOME TO CODESPEEDY! LEARN CHARACTER STREAM!

Byte stream explanation:

Similarly, we have Byte stream.

Firstly, we write a text file named, for example, “input_file_for_byte_stream.txt” having any content, for instance, “WELCOME TO CODESPEEDY! LEARN BYTE STREAM!”.

So, we write the sample code as above to copy the source file “input_file_for_byte_stream.txt” to destination file named, for example, “output_file_for_byte_stream.txt”. After that, we need to compile and execute this sample code.

// To compile the sample code

$ javac codeForByteStream.java 

// To execute the sample code

$ java codeForByteStream 

Hence, on execution, the text file “output_file_for_byte_stream.txt” is obtained. Therefore, it has the content of the source file “input_file_for_byte_stream.txt”. So, it is copied successfully using byte stream in Java.

WELCOME TO CODESPEEDY! LEARN BYTE STREAM!

Different Classes of Character and Byte Streams in Java

Also, we have different classes of both Character and Byte streams in Java. So, we have mentioned about those classes of these two streams:

Character stream:

We have the Reader and Writer classes of the Character streams.

Firstly, the Reader Classes of character stream in Java are:

  1. BufferedReader-  It reads characters from the buffer.
  2. CharArrayReader- It reads characters from a character array.
  3. FileReader- It reads from a file.
  4. FilterReader- It reads from underlying character-input stream.
  5. InputStreamReader- It converts bytes to characters.
  6. PipedReader- It reads from the connected piped output stream.
  7. StringReader- It reads from a string.

Secondly, the Writer Classes of character stream in Java are:

  1. BufferedWriter- It writes characters to a buffer.
  2. FileWriter- It writes to a file.
  3. FilterWriter- It writes characters to the output stream.
  4. CharArrayWriter- It writes characters to a character array.
  5. OutputStreamWriter- It converts from bytes to character.
  6. PipedWriter- It writes to the connected piped input stream.
  7. StringWriter- It writes to a string.

 

Byte stream:

We have the InputStream and OutputStream classes of the Byte streams.

Firstly, the InputStream Classes of byte stream in Java are:

  1. BufferedInputStream- It reads bytes from the buffer.
  2. ByteArrayInputStream- It reads bytes from a byte array.
  3. DataInputStream- It reads Java primitive data types.
  4. FileInputStream- It reads bytes from a file.
  5. FilterInputStream- It reads bytes from other input streams.
  6. ObjectInputStream- It reads objects.
  7. PipedInputStream- It reads from a piped output stream.
  8. SequenceInputStream- It joins multiple input streams and read from the combined stream.

Secondly, the OutputStream Classes of byte stream in Java are:

  1. BufferedOutputStream- It writes bytes into the buffer.
  2. ByteArrayOutputStream- It writes bytes into a byte array.
  3. DataOutputStream- It writes Java primitive data types.
  4. FileOutputStream- It writes bytes to a file.
  5. FilterOutputStream- It writes to other output streams.
  6. ObjectOutputStream- It writes objects.
  7. PipedOutputStream- It writes to a piped output stream.
  8. PrintStream- It prints Java primitive data types.

Leave a Reply

Your email address will not be published. Required fields are marked *