Perfect bhai!
Let's master everything about:
✅ Byte-oriented & Character-oriented Streams
✅ Java I/O Streams
✅ Java Reader/Writer
🔷 1. What is Java I/O?
Java I/O (Input and Output) allows your program to take input from sources like files, keyboard, or network and produce output to
destinations like files, monitor, etc.
🔷 2. Streams in Java (Very Important for Exams)
Java uses the concept of streams to handle I/O.
Streams are sequences of data.
There are two types:
Stream Type For Handling Main Classes Used
Byte-Oriented Binary data (images, files) InputStream / OutputStream
Character-Oriented Text data (characters) Reader / Writer
🔷 3. Byte-Oriented Streams
Used to read/write binary data (image, audio, etc.)
Deals with 8-bit bytes
🔹 Superclasses:
InputStream → Base for reading bytes
OutputStream → Base for writing bytes
🔹 Common Subclasses:
Class Use
FileInputStream Reads bytes from a file
FileOutputStream Writes bytes to a file
BufferedInputStream Reads bytes with buffering
BufferedOutputStream Writes bytes with buffering
DataInputStream Reads Java primitive types
DataOutputStream Writes Java primitive types
🔹 Example:
java Copy Edit
import [Link].*; public class ByteExample { public static void main(String[] args) throws IOException { FileOutputStream fout =
new FileOutputStream("[Link]"); [Link](65); // writes ASCII A [Link](); FileInputStream fin = new
FileInputStream("[Link]"); int i = [Link](); [Link]((char)i); // Output: A [Link](); } }
🔷 4. Character-Oriented Streams
Used to read/write text data
Handles 16-bit Unicode characters
🔹 Superclasses:
Reader → Base for reading characters
Writer → Base for writing characters
🔹 Common Subclasses:
Class Use
FileReader Reads characters from a file
FileWriter Writes characters to a file
BufferedReader Reads text efficiently using buffer
BufferedWriter Writes text efficiently
InputStreamReader Converts byte stream to char stream
Class Use
OutputStreamWriter Converts char stream to byte stream
🔹 Example:
java Copy Edit
import [Link].*; public class CharExample { public static void main(String[] args) throws IOException { FileWriter fw = new
FileWriter("[Link]"); [Link]("Hello"); [Link](); FileReader fr = new FileReader("[Link]"); int ch; while((ch = [Link]())
!= -1){ [Link]((char)ch); } [Link](); } }
🔷 5. Java Reader and Writer (Character Streams)
These are abstract classes meant for reading and writing characters.
🔹 Reader:
int read() : reads one character
int read(char[] cbuf) : reads into char array
🔹 Writer:
void write(int c) : writes one character
void write(String s) : writes string
🔷 6. Buffered Streams (Bonus Tip for Performance)
Buffered streams are used to improve efficiency.
They read/write chunks of data at once.
Use BufferedReader and BufferedWriter for text
Use BufferedInputStream and BufferedOutputStream for binary
java Copy Edit
BufferedReader br = new BufferedReader(new FileReader("[Link]")); BufferedWriter bw = new BufferedWriter(new
FileWriter("[Link]"));
🔷 7. InputStreamReader & OutputStreamWriter
Bridges between byte streams and character streams.
java Copy Edit
InputStreamReader isr = new InputStreamReader([Link]); BufferedReader br = new BufferedReader(isr); String name =
[Link]();