Byte Stream and Character Stream in Java
images, audio, or video.
InputStream and OutputStream and include child classes like FileInputStream and FileOutputStream.
.txt, .java, or .csv.
Reader and Writer and include child classes like FileReader and FileWriter.
| Feature | Byte Stream | Character Stream |
|---|---|---|
| Data Unit | Byte Stream reads or writes bytes (8-bit data) and is suitable for binary files like images, audio, and video. | Character Stream reads or writes characters (16-bit Unicode data) and is suitable for text files like .txt, .java, or .csv. |
| Abstract Classes | Based on InputStream and OutputStream which provide methods to read/write bytes. |
Based on Reader and Writer which provide methods to read/write characters. |
| Child Classes | Important classes include FileInputStream, FileOutputStream, BufferedInputStream, DataOutputStream, etc. |
Important classes include FileReader, FileWriter, BufferedReader, BufferedWriter, PrintWriter, etc. |
| Used For | Used for reading or writing binary files like images, audio, and videos. | Used for reading or writing text files like .txt, .java, .csv, and other textual data. |
| Conversion Requirement | Manual conversion is required when reading text files; bytes need to be cast to characters using (char). |
No conversion required; characters are read/written directly and encoding is handled automatically. |
| Encoding Awareness | Not aware of character encoding; only deals with raw bytes. | Aware of character encoding and handles Unicode characters correctly. |
| Efficiency | Efficient for raw binary data but less convenient for text files. | Efficient for text files, especially when used with buffered classes like BufferedReader and BufferedWriter. |
| Package | All Byte Stream classes belong to the java.io package. |
All Character Stream classes belong to the java.io package. |
Your feedback helps us grow! If there's anything we can fix or improve, please let us know.
Weβre here to make our tutorials better based on your thoughts and suggestions.