← Back to Core Java Course | Chapter 12: File I/O | Lesson 6 of 9

Java में BufferedReader और BufferedWriter

BufferedWriter और BufferedReader files में text की lines लिखने और पढ़ने के तेज़ तरीके हैं, एक spoon के बजाय एक bucket उपयोग करने जैसा। वे data move करने से पहले collect करते हैं।
Syntax
java
try (BufferedReader reader = new BufferedReader(new FileReader("file_name"))) {
    String line = reader.readLine();
}

Writing Lines with BufferedWriter

BufferedWriter किसी और writer (जैसे FileWriter) को wrap करता है और memory में output batch करता है, हर single write call पर flush करने के बजाय बड़े chunks में disk पर flush करते हुए — यह कई lines लिखने के लिए काफी तेज़ है।

उदाहरण: Writing Lines with BufferedWriter

java
// Import java.io.FileWriter so it can be used by its short name
import java.io.FileWriter;
// Import java.io.BufferedWriter so it can be used by its short name
import java.io.BufferedWriter;
import java.io.IOException;
// Define the class Main
public class Main {
	// Program entry point: the JVM starts running here
	public static void main(String[] args) throws IOException {
		// Create a new BufferedWriter object and store it in writer
		BufferedWriter writer = new BufferedWriter(new FileWriter("notes.txt"));
		writer.write("Line one");
		writer.newLine();
		writer.write("Line two");
		writer.close();
		// Print a line to the console
		System.out.println("Written");
	}
}

Reading Lines with BufferedReader

BufferedReader किसी और reader को wrap करता है और readLine() जोड़ता है, जो एक समय में text की पूरी line return करता है (या end-of-file पर null) — एक समय में एक character पढ़ने से कहीं ज़्यादा convenient।

उदाहरण: Reading Lines with BufferedReader

java
// Import java.io.FileWriter so it can be used by its short name
import java.io.FileWriter;
// Import java.io.BufferedReader so it can be used by its short name
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
// Define the class Main
public class Main {
	// Program entry point: the JVM starts running here
	public static void main(String[] args) throws IOException {
		// Create a new FileWriter object and store it in writer
		FileWriter writer = new FileWriter("notes.txt");
		writer.write("Hello\nWorld");
		writer.close();
		// Create a new BufferedReader object and store it in reader
		BufferedReader reader = new BufferedReader(new FileReader("notes.txt"));
		String line;
		// Repeat while (line = reader.readLine()) != null is true
		while ((line = reader.readLine()) != null) {
			// Print a line to the console
			System.out.println(line);
		}
		reader.close();
	}
}

Flushing Buffered Streams

Buffered streams तुरंत disk में नहीं लिखते; flush() call करना अभी किसी भी buffered data को बाहर जाने पर मजबूर करता है, और stream बंद करना अपने आप flush करता है, इसलिए unflushed data सिर्फ तभी असल में खोता है अगर program बंद होने से पहले crash हो जाए।

उदाहरण: Flushing Buffered Streams

java
import java.io.FileWriter;
import java.io.BufferedWriter;
import java.io.IOException;
public class Main {
	public static void main(String[] args) throws IOException {
		BufferedWriter writer = new BufferedWriter(new FileWriter("notes.txt"));
		writer.write("Buffered data");
		writer.flush(); // forces it out now
		writer.close();
		System.out.println("Flushed and closed");
	}
}

Exception Handling

File I/O लगभग किसी भी point पर checked IOExceptions throw कर सकता है — एक missing file, एक full disk, एक permissions error — इसलिए buffered reads और writes को unbuffered वालों जैसी ही try-catch या try-with-resources discipline चाहिए।

उदाहरण: Exception Handling

java
// Import java.io.BufferedReader so it can be used by its short name
import java.io.BufferedReader;
// Import java.io.FileReader so it can be used by its short name
import java.io.FileReader;
import java.io.IOException;
// Define the class Main
public class Main {
	// Program entry point: the JVM starts running here
	public static void main(String[] args) {
		// Try code that might throw an exception
		try (BufferedReader reader = new BufferedReader(new FileReader("missing.txt"))) {
			// Print a line to the console
			System.out.println(reader.readLine());
		// Handle IOException if it was thrown
		} catch (IOException e) {
			// Print a line to the console
			System.out.println("Caught: " + e.getClass().getSimpleName());
		}
	}
}

Data Aggregation

एक पूरी file को line-by-line एक List<String> में पढ़ना या numeric lines से एक running total बनाना BufferedReader के typical उपयोग हैं, क्योंकि readLine() naturally input को processable chunks में तोड़ता है।

उदाहरण: Data Aggregation

java
// Import java.io.FileWriter so it can be used by its short name
import java.io.FileWriter;
// Import java.io.BufferedReader so it can be used by its short name
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
// Define the class Main
public class Main {
	// Program entry point: the JVM starts running here
	public static void main(String[] args) throws IOException {
		// Create a new FileWriter object and store it in writer
		FileWriter writer = new FileWriter("numbers.txt");
		writer.write("10\n20\n30");
		writer.close();
		// Create a new BufferedReader object and store it in reader
		BufferedReader reader = new BufferedReader(new FileReader("numbers.txt"));
		// Declare total and set it to 0
		int total = 0;
		String line;
		// Repeat while (line = reader.readLine()) != null is true
		while ((line = reader.readLine()) != null) {
			total += Integer.parseInt(line);
		}
		reader.close();
		System.out.println(total);
	}
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. एक BufferedWriter पर flush() या close() call करना भूल जाना, इसलिए text खो जाता है।
  2. यह भूल जाना कि readLine() file के आखिर में null return करता है।
  3. newLine() भूल जाना और lines साथ जुड़ जाना।

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.