📁
File I/O

File Handling / Java I/O

Reading & Writing Files
💡 File handling एक diary लिखने/पढ़ने जैसा है — FileWriter से diary में लिखते हो, BufferedReader से diary पढ़ते हो। try-with-resources एक smart helper है जो diary बंद करना ख़ुद याद रखता है।

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());
}
📁
File handling एक diary लिखने/पढ़ने जैसा है — FileWriter से diary में लिखते हो, BufferedReader से diary पढ़ते हो। try-with-resources एक smart helper है जो diary बंद करना ख़ुद याद रखता है।
1 / 6
इस page में (4 subtopics)

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.

StreamsReaders/Writers
Data typeRaw bytesCharacters (text)
Encoding handle karta hai?NahiHaan (UTF-8, etc.)
Example classesFileInputStream, FileOutputStreamFileReader, FileWriter
Kab use kareinImages, videos, binaryText 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");
}
💡Tip: Hamesha BufferedReader/BufferedWriter ka use karo directly FileReader/FileWriter ki jagah — performance mein bahut fark padta hai bade files ke liye, aur code bhi utna hi simple rehta hai.

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/false
⚠️Common Mistake: Files.readAllLines() poori file memory mein load kar leta hai — bahut badi files (GBs) ke liye ye risky hai (OutOfMemoryError). Bade files ke liye BufferedReader se line-by-line stream karke padho.

File 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").