Java NIO परिचय
In this page:
What is NIO?
Java NIO (New I/O) buffers, channels, और selectors के इर्द-गिर्द बना एक वैकल्पिक I/O API है, जिसे classic stream-based java.io API से ज़्यादा throughput और scalability के लिए डिज़ाइन किया गया है, खासतौर पर एक साथ कई connections handle करते समय।
उदाहरण: What is NIO?
// Define the class Main
public class Main {
// Program entry point: the JVM starts running here
public static void main(String[] args) {
// Print a line to the console
System.out.println("NIO: buffers, channels, and selectors for higher throughput than java.io");
}
}
Login to try C/C++/Java/PHP code in the editor
Buffers
एक Buffer data का एक fixed-size container है जिसमें position, limit, और capacity होती है जो track करते हैं कि कितना लिखा या पढ़ा जा चुका है, और flip() call करने से यह filling mode से draining mode में बदल जाता है ताकि वही data वापस पढ़ा जा सके।
उदाहरण: Buffers
import java.nio.ByteBuffer;
public class Main {
public static void main(String[] args) {
ByteBuffer buffer = ByteBuffer.allocate(10);
buffer.put((byte) 65);
buffer.flip(); // switches from filling mode to draining mode
System.out.println(buffer.get());
}
}
Login to try C/C++/Java/PHP code in the editor
Channels
एक Channel किसी file या socket जैसे I/O source के एक खुले connection को represent करता है, और किसी stream के विपरीत, data एक channel के ज़रिए किसी Buffer में या उससे बाहर बहता है, अक्सर एक ही operation में बड़े blocks of data move करते हुए।
उदाहरण: Channels
import java.nio.channels.*;
import java.nio.ByteBuffer;
import java.io.*;
public class Main {
public static void main(String[] args) throws Exception {
File file = new File("data.txt");
try (FileOutputStream fos = new FileOutputStream(file); FileChannel channel = fos.getChannel()) {
ByteBuffer buffer = ByteBuffer.wrap("hello".getBytes());
channel.write(buffer); // data flows through the channel into/out of a Buffer
}
file.delete();
System.out.println("Written through a channel");
}
}
Login to try C/C++/Java/PHP code in the editor
Selectors
एक Selector एक single thread को data पढ़ने के लिए तैयार होने जैसे events के लिए एक साथ कई channels monitor करने देता है, जो वह तरीका है जिससे NIO per-connection एक thread की ज़रूरत के बिना हज़ारों connections handle करने देता है।
उदाहरण: Selectors
import java.nio.channels.Selector;
public class Main {
public static void main(String[] args) throws Exception {
Selector selector = Selector.open(); // one thread can monitor many channels
System.out.println("Selector open: " + selector.isOpen());
}
}
Login to try C/C++/Java/PHP code in the editor
NIO vs Classic I/O
Classic java.io streams blocking होती हैं और हर connection के लिए एक dedicated thread पर एक समय में एक byte या chunk data पढ़ती हैं, जबकि NIO का buffer-and-channel model non-blocking चल सकता है, कहीं कम threads को कहीं ज़्यादा concurrent I/O operations manage करने देते हुए।
उदाहरण: NIO vs Classic I/O
// Define the class Main
public class Main {
// Program entry point: the JVM starts running here
public static void main(String[] args) {
// Print a line to the console
System.out.println("Classic I/O: blocking, one thread per connection");
// Print a line to the console
System.out.println("NIO: non-blocking, fewer threads manage more connections");
}
}
Login to try C/C++/Java/PHP code in the editor
- किसी buffer में लिखने के बाद और उससे पढ़ने से पहले
buffer.flip()भूल जाना, जिससे आप गलत data पढ़ते हैं। - किसी buffer को reuse करने से पहले
clear()याcompact()call न करना, जिससे अगली read के पास जगह नहीं होती या वह unread data को overwrite कर देती है। - यह मान लेना कि एक
readपूरा buffer भर देगा, जबकि यह कम bytes return कर सकता है, या stream के अंत में-1।
Chapter Quiz — Complete all 5 topics to unlock
0/5 topics done
Complete these topics first: