Byte streams process data byte by byte (8 bits). For example FileInputStream is used to read from source and FileOutputStream to write to the destination.
import java.io.*;
public class Byte_Stream {
public static void main(String[] args) {
try {
//write
FileOutputStream output=new FileOutputStream("C:\\Users\\Raazu\\Desktop\\bca.txt");
int content=1011011; output.write((byte) content);
System.out.println
("Byte Stream written successfully !"); output.close();
//read
FileInputStream input=new FileInputStream("C:\\Users\\Raazu\\Desktop\\bca.txt");
int data=input.read(); System.out.println((char) data); input.close();
}catch(Exception e) { System.out.println(e);
}
}
}
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 A 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