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

Java के I/O Streams

एक stream data का एक continuous flow represent करता है program में या बाहर, और Java में हर file-reading या file-writing operation आखिरकार एक input या output stream के ऊपर बना है।

What is a Stream?

Java में एक stream data का एक continuous flow represent करता है, या तो एक source से program में आता हुआ जैसे एक file या network connection, या program से एक destination की ओर जाता हुआ, और हर file operation एक input या output stream के ऊपर बना है।

उदाहरण: What is a Stream?

java
import java.io.FileWriter;
import java.io.IOException;
public class Main {
	public static void main(String[] args) throws IOException {
		FileWriter writer = new FileWriter("data.txt"); // stream: flow of data out
		writer.write("Hello");
		writer.close();
		System.out.println("Written");
	}
}

Byte Streams vs Character Streams

Java streams को दो families में बांटता है: FileInputStream और FileOutputStream जैसे byte streams एक समय में एक byte raw binary data handle करते हैं, जबकि FileReader और FileWriter जैसे character streams text handle करते हैं और character encoding अपने आप manage करते हैं।

उदाहरण: Byte Streams vs Character Streams

java
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
public class Main {
	public static void main(String[] args) throws IOException {
		FileOutputStream byteStream = new FileOutputStream("bytes.dat"); // raw binary
		byteStream.write(65);
		byteStream.close();
		FileWriter charStream = new FileWriter("text.txt"); // text
		charStream.write("Hello");
		charStream.close();
	}
}

Input Streams and Output Streams

एक InputStream read() जैसे methods के through program में आने वाली data देता है, जो data का आखिर पहुंचने पर -1 return करता है, जबकि एक OutputStream data को इसके destination पर भेजने के लिए एक write() method देता है।

उदाहरण: Input Streams and Output Streams

java
import java.io.FileWriter;
import java.io.FileReader;
import java.io.IOException;
public class Main {
	public static void main(String[] args) throws IOException {
		FileWriter writer = new FileWriter("data.txt");
		writer.write("Hi");
		writer.close();
		FileReader reader = new FileReader("data.txt");
		int c;
		while ((c = reader.read()) != -1) { // -1 marks end of data
			System.out.print((char) c);
		}
		reader.close();
	}
}

Stream Buffering

एक समय में एक byte या character पढ़ना या लिखना धीमा है क्योंकि हर call एक अलग disk operation trigger कर सकती है, इसलिए BufferedReader और BufferedWriter जैसे buffered streams असली I/O operations की संख्या घटाने के लिए memory में data batch करते हैं।

उदाहरण: Stream Buffering

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("data.txt")); // batches writes
		writer.write("Hello");
		writer.close();
		System.out.println("Written with buffering");
	}
}

Closing Streams Properly

हर stream को underlying file handle या system resource release करने के लिए आखिरकार बंद किया जाना चाहिए, और Java का try-with-resources statement यह अपने आप और सुरक्षित रूप से करता है यहां तक कि जब एक exception read या write को interrupt करे।

उदाहरण: Closing Streams Properly

java
import java.io.FileWriter;
import java.io.IOException;
public class Main {
	public static void main(String[] args) throws IOException {
		try (FileWriter writer = new FileWriter("data.txt")) { // try-with-resources auto-closes
			writer.write("Hello");
		}
		System.out.println("Closed automatically");
	}
}
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. Text के लिए एक byte stream उपयोग करना, जब Reader और Writer classes characters के लिए हैं।
  2. Streams बंद न करना, जो resources leak कर सकता है।
  3. Buffered output flush न करना, इसलिए data नहीं लिखी जा सकती।

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.