File Handling / Java I/O
java.io package files के साथ काम करने के tools देता है — FileReader/FileWriter characters के लिए, BufferedReader fast line-by-line reading के लिए।
try-with-resources resource को अपने आप close कर देता है जब block ख़त्म हो — file/connection खुली रह जाने की ग़लती से बचाता है।
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());
}- FileReader/Writer = characters पढ़ना/लिखना
- BufferedReader = fast line-by-line reading
- try-with-resources = auto-close
Java I/O दो families में divide होता है: Streams (InputStream/OutputStream) raw bytes के लिए — images, videos, कोई भी binary data, जहाँ encoding का कोई concept नहीं है (बस 0s और 1s)। Readers/Writers (FileReader/FileWriter) specifically characters/text के लिए — encoding (UTF-8 आदि) automatically handle करते हैं, ताकि special characters (जैसे Hindi text) सही से पढ़े/लिखे जाएं।
Rule of thumb: Text files (.txt, .csv, .java) के लिए Reader/Writer use करो, binary files (.jpg, .mp3, .zip) के लिए Stream use करो।
| Streams | Readers/Writers | |
|---|---|---|
| Data type | Raw bytes | Characters (text) |
| Encoding handle करता है? | नहीं | हाँ (UTF-8, etc.) |
| Example classes | FileInputStream, FileOutputStream | FileReader, FileWriter |
| कब use करें | Images, videos, binary | Text files |
FileWriter सीधा file में लिखता है — हर write() call disk तक जाता है, जो slow हो सकता है अगर छोटे-छोटे pieces में बार-बार लिख रहे हो। BufferedWriter इसको wrap करके performance बढ़ाता है — data को memory buffer में जमा करके एक साथ disk पर लिखता है (बार-बार disk access expensive होता है, memory access सस्ता)।
newLine() platform-independent line break देता है (Windows और Linux/Mac में line-ending characters अलग होते हैं — \r\n vs \n — newLine() यह automatically सही handle करता है)।
try (BufferedWriter bw = new BufferedWriter(new FileWriter("notes.txt"))) {
bw.write("Hello Java!");
bw.newLine();
bw.write("Second line");
}Java 7+ ने java.nio.file.Files class दिया जो बहुत simple, modern API देता है — Files.readAllLines(path) पूरी file एक List<String> में पढ़ लेता है एक line में (छोटी-medium files के लिए perfect, पूरा content memory में आ जाता है)। Files.write(path, lines) लिखता है।
Files.exists(path), Files.delete(path), Files.copy(source, target) जैसी utility methods भी हैं जो पुराने java.io API में काफी ज़्यादा code मांगती थीं।
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 दे सकते हैं — यह एक checked exception है, handle करना ज़रूरी है (compiler force करेगा)।
try-with-resources use करो ताकि file automatically close हो (चाहे exception आए या न आए), और catch block में meaningful error message दो, silently ignore मत करो — user को बताओ exactly क्या गलत हुआ (जैसे "File notes.txt नहीं मिली" बजाय generic "Error हुआ")।