Reading and Writing Data: InputStream, OutputStream, and Buffered I/O
Reading and Writing Data: InputStream, OutputStream, and Buffered I/O
Introduction
Java में data को पढ़ने (read) और लिखने (write) के लिए InputStream और OutputStream classes का उपयोग किया जाता है। ये classes java.io package का हिस्सा हैं और binary data को handle करने के लिए design की गई हैं। जब भी हमें किसी file, network connection या memory buffer से data transfer करना होता है, तो Streams का उपयोग किया जाता है।
What is Stream?
Stream का मतलब है “data का continuous flow” — जैसे पानी की धार। Java में Stream दो प्रकार की होती हैं:
- InputStream – data को read करने के लिए।
- OutputStream – data को write करने के लिए।
Stream एक ऐसा channel होता है जो source और destination के बीच data को transfer करने का काम करता है।
InputStream
InputStream एक abstract class है जो input data को read करने के लिए methods provide करती है। यह bytes में data को पढ़ती है। इसका सबसे common use file या network socket से data पढ़ने में होता है।
Common Methods of InputStream
| Method | Description |
|---|---|
int read() | एक single byte data read करता है और उसे return करता है। |
int read(byte[] b) | Array में multiple bytes read करता है। |
int read(byte[] b, int off, int len) | Array के specific हिस्से में data read करता है। |
void close() | Stream को close करता है और resource free करता है। |
Example of InputStream
नीचे एक simple example दिया गया है जहाँ file से data read किया गया है:
import java.io.FileInputStream;
import java.io.InputStream;
public class ReadExample {
public static void main(String[] args) {
try {
InputStream input = new FileInputStream("data.txt");
int i = input.read();
while(i != -1) {
System.out.print((char)i);
i = input.read();
}
input.close();
} catch(Exception e) {
System.out.println(e);
}
}
}
OutputStream
OutputStream class data को किसी destination में write करने के लिए use की जाती है। यह भी bytes के form में data handle करती है।
Common Methods of OutputStream
| Method | Description |
|---|---|
void write(int b) | एक byte data write करता है। |
void write(byte[] b) | पूरा byte array write करता है। |
void write(byte[] b, int off, int len) | Array के कुछ bytes write करता है। |
void flush() | Buffer में बचा हुआ data output में भेज देता है। |
void close() | Stream को बंद कर देता है। |
Example of OutputStream
import java.io.FileOutputStream;
import java.io.OutputStream;
public class WriteExample {
public static void main(String[] args) {
try {
OutputStream output = new FileOutputStream("output.txt");
String data = "Hello Java Stream!";
byte[] bytes = data.getBytes();
output.write(bytes);
output.close();
System.out.println("Data written successfully!");
} catch(Exception e) {
System.out.println(e);
}
}
}
Buffered I/O
Java में Buffered I/O classes का use performance improve करने के लिए किया जाता है। ये memory buffer का उपयोग करके read/write operations को तेज़ बनाती हैं।
BufferedInputStream और BufferedOutputStream
BufferedInputStream और BufferedOutputStream byte-based buffered streams हैं। ये data को temporarily buffer में store करते हैं ताकि बार-बार disk access न करना पड़े।
BufferedInputStream Example
import java.io.*;
public class BufferedReadExample {
public static void main(String[] args) {
try {
FileInputStream fin = new FileInputStream("data.txt");
BufferedInputStream bin = new BufferedInputStream(fin);
int i;
while((i = bin.read()) != -1){
System.out.print((char)i);
}
bin.close();
fin.close();
} catch(Exception e) {
System.out.println(e);
}
}
}
BufferedOutputStream Example
import java.io.*;
public class BufferedWriteExample {
public static void main(String[] args) {
try {
FileOutputStream fout = new FileOutputStream("buffered.txt");
BufferedOutputStream bout = new BufferedOutputStream(fout);
String s = "Buffered output example in Java";
byte b[] = s.getBytes();
bout.write(b);
bout.flush();
bout.close();
fout.close();
System.out.println("Buffered data written successfully.");
} catch(Exception e) {
System.out.println(e);
}
}
}
Difference Between InputStream/OutputStream and Buffered Streams
| Feature | InputStream/OutputStream | Buffered Streams |
|---|---|---|
| Performance | Slow | Fast (uses buffer) |
| Memory Use | Direct I/O | Uses internal buffer |
| Read/Write | One byte at a time | Multiple bytes at once |
| Efficiency | Low | High |
Why Buffered I/O is Important?
- Buffered I/O read और write operations को optimize करता है।
- File access की frequency कम हो जाती है।
- Large files के साथ performance काफी बेहतर होती है।
- CPU utilization efficient रहता है।
Practical Usage in Real World
- File Handling: File data को read/write करने में Buffered I/O fastest तरीका है।
- Network Programming: Socket communication में Buffered Streams data transfer को तेज़ करते हैं।
- Logging Systems: BufferedOutputStream का use log files में होता है ताकि writing delay न हो।
Exam Oriented Key Points
- InputStream → data पढ़ने के लिए।
- OutputStream → data लिखने के लिए।
- BufferedInputStream → performance बढ़ाता है।
- close() method हमेशा use करना चाहिए।
- Streams byte-oriented होती हैं जबकि Reader/Writer character-oriented होते हैं।
Quick Notes for Revision
| Concept | Purpose |
|---|---|
| InputStream | Read binary data from source |
| OutputStream | Write binary data to destination |
| BufferedInputStream | Faster reading using internal buffer |
| BufferedOutputStream | Faster writing using internal buffer |
| close() | Releases system resources |
| flush() | Clears buffer and writes remaining data |