File Handling / Java I/O
The java.io package gives tools for working with files — FileReader/FileWriter for characters, BufferedReader for fast line-by-line reading.
try-with-resources automatically closes a resource once the block finishes — it protects you from the mistake of leaving a file/connection open.
try (BufferedReader br = new BufferedReader(new FileReader("notes.txt"))) {
String line = br.readLine();
System.out.println(line);
} catch (IOException e) {
System.out.println("File error: " + e.getMessage());
}Java I/O do families mein divide hota hai: Streams (InputStream/OutputStream) raw bytes ke liye — images, videos, koi bhi binary data, jaha encoding ka koi concept nahi hai (bas 0s aur 1s). Readers/Writers (FileReader/FileWriter) specifically characters/text ke liye — encoding (UTF-8 etc.) automatically handle karte hain, taaki special characters (jaise Hindi text) sahi se padhe/likhe jaayein.
Rule of thumb: Text files (.txt, .csv, .java) ke liye Reader/Writer use karo, binary files (.jpg, .mp3, .zip) ke liye Stream use karo.
| Streams | Readers/Writers | |
|---|---|---|
| Data type | Raw bytes | Characters (text) |
| Encoding handle karta hai? | Nahi | Haan (UTF-8, etc.) |
| Example classes | FileInputStream, FileOutputStream | FileReader, FileWriter |
| Kab use karein | Images, videos, binary | Text files |
FileWriter seedha file mein likhता hai — har write() call disk tak jaata hai, jo slow ho sakta hai agar chhote-chhote pieces mein baar-baar likh rahe ho. BufferedWriter isko wrap karke performance badhata hai — data ko memory buffer mein jama karke ek saath disk par likhता hai (baar-baar disk access expensive hota hai, memory access sasta).
newLine() platform-independent line break deta hai (Windows aur Linux/Mac mein line-ending characters alag hote hain — \r\n vs \n — newLine() ye automatically sahi handle karta hai).
try (BufferedWriter bw = new BufferedWriter(new FileWriter("notes.txt"))) {
bw.write("Hello Java!");
bw.newLine();
bw.write("Second line");
}Java 7+ ne java.nio.file.Files class diya jo bahut simple, modern API deta hai — Files.readAllLines(path) poori file ek List<String> mein padh leta hai ek line mein (chhoti-medium files ke liye perfect, poora content memory mein aa jaata hai). Files.write(path, lines) likhta hai.
Files.exists(path), Files.delete(path), Files.copy(source, target) jaisi utility methods bhi hain jo purane java.io API mein kaafi zyada code maangti thi.
List<String> lines = Files.readAllLines(Paths.get("notes.txt"));
Files.write(Paths.get("output.txt"), lines);
System.out.println(Files.exists(Paths.get("notes.txt"))); // true/falseFile operations (missing file, permission denied, disk full, network drive disconnect) IOException de sakte hain — ye ek checked exception hai, handle karna zaroori hai (compiler force karega).
try-with-resources use karo taaki file automatically close ho (chahe exception aaye ya na aaye), aur catch block mein meaningful error message do, silently ignore mat karo — user ko batao exactly kya galat hua (jaise "File notes.txt nahi mili" bajaye generic "Error hua").