Byte Stream Vs Character Stream In Java


A stream is a sequence of data generated by the input source and consumed by the output destination. There are two types of I/O streams in Java. One is byte stream and another one is character stream. Byte stream processes the data byte by byte i.e 8 bits at a time. Character stream processes the data character by character. Characters in Java are from Unicode set and are of usually 16 bits in size. Hence, character stream in Java usually processes the data of 16 bits at a time. In this post, we will see the differences between byte stream Vs character stream in Java and what are the Java classes and interfaces which implement these two streams.

Byte Stream In Java :

Byte stream reads and writes the data byte by byte i.e 8 bits maximum at a time. This stream is most suitable to process the binary files like image files, audio files, video files, executable files etc…

All byte stream classes in Java are of type InputStream and OutputStream.

FileInputStream, BufferedInputStream, DataInputStream, ObjectInputStream, ByteArrayInputStream are some byte stream classes in Java which are used for input i.e for reading data from source file.

FileOutputStream, BufferedOutputStream, DataOutputStream, ObjectOutputStream, ByteArrayOutputStream are some byte stream classes in Java which are used for output i.e for writing data to destination file.

As you notice, names of byte streams typically ends with either InputStream or OutputStream.

Below is an example which reads data from InputFile.jpg and writes to OutputFile.jpg using byte stream classes.

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

public class JavaIOStreams 
{
	public static void main(String[] args) 
	{
		FileInputStream fis = null;
		FileOutputStream fos = null;
		
		try 
		{
			fis = new FileInputStream("InputFile.jpg");
			fos = new FileOutputStream("OutputFile.jpg");
			
			int temp;
			
			while ( (temp = fis.read()) != -1) 
			{
				fos.write(temp);
			}
		} 
		catch (IOException e) 
		{
			e.printStackTrace();
		}
		finally 
		{
			if (fis != null) 
			{
				try 
				{
					fis.close();
				} 
				catch (IOException e) 
				{
					e.printStackTrace();
				}
			}
			
			if (fos != null)
			{
				try 
				{
					fos.close();
				} 
				catch (IOException e) 
				{
					e.printStackTrace();
				}
			}
		}

	}
}

Character Stream In Java :

Character stream reads and writes the data character by character. As already said, characters in Java are from Unicode characters set and are of 16 bits in size. Hence you can say that, character streams in Java read and write data of 16 bits maximum at a time.

Character streams are mainly used to process text files.

All character stream classes in Java are of type Reader and Writer.

FileReader, BufferedReader, CharacterArrayReader, PipedReader, InputStreamReader are some character stream classes in Java which are used for input i.e for reading a file.

FileWriter, BufferedWriter, CharacterArrayWriter, PipedWriter, InputStreamWriter are some character stream classes in Java which are used for output i.e for writing a file.

You can notice that all names of character streams end with either Reader or Writer.

Below is an example which reads data from InputFile.txt and writes to OutputFile.txt using character stream classes.

import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

public class JavaIOStreams 
{
	public static void main(String[] args) 
	{
		FileReader reader = null;
		FileWriter writer = null;
		
		try 
		{
			reader = new FileReader("InputFile.txt");
			writer = new FileWriter("OutputFile.txt");
			
			int temp;
			
			while ( (temp = reader.read()) != -1)
			{
				writer.write(temp);
			}
		} 
		catch (IOException e) 
		{
			e.printStackTrace();
		}
		finally 
		{
			if (reader != null) 
			{
				try 
				{
					reader.close();
				} 
				catch (IOException e) 
				{
					e.printStackTrace();
				}
			}
			
			if (writer != null)
			{
				try 
				{
					writer.close();
				} 
				catch (IOException e) 
				{
					e.printStackTrace();
				}
			}
		}

	}
}

Byte Stream Vs Character Stream In Java :

Byte StreamCharacter Stream
They process the data byte by byte.They process the data character by character.
They read/write data 8 bits maximum at a time.They read/write data 16 bits maximum at a time.
They are most suitable to process binary files.They are most suitable to process text files.
All byte stream classes in Java are descendants of InputStream and OutputStream.All character stream classes in Java are descendants of Reader and Writer.

Below image shows some important byte stream classes and character stream classes in Java.

Byte Stream Vs Character Stream In Java

Also Read :


Leave a Reply