Streams Overview
Java I/O is based on streams โ a sequence of data flowing from a source to a destination. All I/O classes are in the java.io package.
| Category | Data Unit | Base Classes | Class Names End With |
|---|---|---|---|
| Byte Stream | 1 byte (8 bits) | InputStream, OutputStream | ...Stream |
| Character Stream | 2 bytes (Unicode) | Reader, Writer | ...Reader / ...Writer |
๐ Key PointUse byte streams for binary data (images, audio, video). Use character streams for text files (they handle Unicode automatically).
Byte Streams
import java.io.*;
public class ByteStreamDemo {
public static void main(String[] args) throws IOException {
// Writing bytes
FileOutputStream fos = new FileOutputStream("data.bin");
fos.write(65); // writes byte value 65 ('A')
fos.write(new byte[]{72,101,108,108,111}); // "Hello"
fos.close();
// Reading bytes
FileInputStream fis = new FileInputStream("data.bin");
int b;
while ((b = fis.read()) != -1) {
System.out.print((char) b);
}
fis.close();
}
}Character Streams
import java.io.*;
public class CharStreamDemo {
public static void main(String[] args) throws IOException {
// Writing characters
FileWriter fw = new FileWriter("notes.txt");
fw.write("Java I/O is simple!
");
fw.write("Character streams handle Unicode.");
fw.close();
// Reading characters
FileReader fr = new FileReader("notes.txt");
int ch;
while ((ch = fr.read()) != -1) {
System.out.print((char) ch);
}
fr.close();
}
}BufferedReader & BufferedWriter
Buffered streams wrap other streams and add an internal buffer, drastically improving I/O performance by reducing the number of disk operations.
import java.io.*;
public class BufferedDemo {
public static void main(String[] args) throws IOException {
// Buffered writing
BufferedWriter bw = new BufferedWriter(new FileWriter("output.txt"));
bw.write("Line 1: Java is powerful");
bw.newLine();
bw.write("Line 2: I/O is efficient");
bw.flush(); // ensure buffer is written to disk
bw.close();
// Buffered reading โ line by line
BufferedReader br = new BufferedReader(new FileReader("output.txt"));
String line;
while ((line = br.readLine()) != null) {
System.out.println(line);
}
br.close();
}
}๐ Key Point
readLine() returns null at end of file. Always call close() to release file handles, or use try-with-resources (Java 7+).File Class
The File class represents a file or directory path. It does NOT read or write content โ it works with file metadata.
import java.io.File;
File f = new File("output.txt");
System.out.println(f.exists()); // true/false
System.out.println(f.getName()); // output.txt
System.out.println(f.length()); // size in bytes
System.out.println(f.isFile()); // true
System.out.println(f.isDirectory()); // false
// Create directory
new File("myFolder").mkdir();
// List files in a directory
File dir = new File(".");
for (String name : dir.list()) System.out.println(name);Append Mode & try-with-resources
// Append to existing file (second argument = true)
FileWriter fw = new FileWriter("log.txt", true);
fw.write("New entry appended\n");
fw.close();
// try-with-resources โ auto closes stream (Java 7+)
try (BufferedReader br = new BufferedReader(new FileReader("log.txt"))) {
String line;
while ((line = br.readLine()) != null)
System.out.println(line);
} // br.close() called automatically๐ Key PointAlways prefer try-with-resources over manual
close() calls โ it is safer and cleaner.Download PDF Notes
Save these notes as a PDF for offline reference