Java के I/O Streams
In this page:
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?
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");
}
}
Login to try C/C++/Java/PHP code in the editor
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
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();
}
}
Login to try C/C++/Java/PHP code in the editor
Input Streams and Output Streams
एक InputStream read() जैसे methods के through program में आने वाली data देता है, जो data का आखिर पहुंचने पर -1 return करता है, जबकि एक OutputStream data को इसके destination पर भेजने के लिए एक write() method देता है।
उदाहरण: Input Streams and Output Streams
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();
}
}
Login to try C/C++/Java/PHP code in the editor
Stream Buffering
एक समय में एक byte या character पढ़ना या लिखना धीमा है क्योंकि हर call एक अलग disk operation trigger कर सकती है, इसलिए BufferedReader और BufferedWriter जैसे buffered streams असली I/O operations की संख्या घटाने के लिए memory में data batch करते हैं।
उदाहरण: Stream Buffering
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");
}
}
Login to try C/C++/Java/PHP code in the editor
Closing Streams Properly
हर stream को underlying file handle या system resource release करने के लिए आखिरकार बंद किया जाना चाहिए, और Java का try-with-resources statement यह अपने आप और सुरक्षित रूप से करता है यहां तक कि जब एक exception read या write को interrupt करे।
उदाहरण: Closing Streams Properly
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");
}
}
Login to try C/C++/Java/PHP code in the editor
- Text के लिए एक byte stream उपयोग करना, जब
ReaderऔरWriterclasses characters के लिए हैं। - Streams बंद न करना, जो resources leak कर सकता है।
- Buffered output flush न करना, इसलिए data नहीं लिखी जा सकती।
Chapter Quiz — Complete all 9 topics to unlock
0/9 topics done
Complete these topics first: