📁
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
⚡ झट से Recap
  • FileReader/Writer = characters पढ़ना/लिखना
  • BufferedReader = fast line-by-line reading
  • try-with-resources = auto-close
इस page में (4 subtopics)

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 करो।

StreamsReaders/Writers
Data typeRaw bytesCharacters (text)
Encoding handle करता है?नहींहाँ (UTF-8, etc.)
Example classesFileInputStream, FileOutputStreamFileReader, FileWriter
कब use करेंImages, videos, binaryText 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");
}
💡Tip: हमेशा BufferedReader/BufferedWriter का use करो directly FileReader/FileWriter की जगह — performance में बहुत फर्क पड़ता है बड़ी files के लिए, और code भी उतना ही simple रहता है।

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/false
⚠️Common Mistake: Files.readAllLines() पूरी file memory में load कर लेता है — बहुत बड़ी files (GBs) के लिए यह risky है (OutOfMemoryError)। बड़ी files के लिए BufferedReader से line-by-line stream करके पढ़ो।

File 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 हुआ")।