← Back to Advanced Java Course | Chapter 13: Java NIO | Lesson 1 of 5

Java NIO परिचय

Java NIO buffers, channels, और selectors के इर्द-गिर्द बना एक वैकल्पिक I/O API है, जिसे classic stream-based java.io API से ज़्यादा throughput और scalability के लिए डिज़ाइन किया गया है।

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?

java
// 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");
	}
}

Buffers

एक Buffer data का एक fixed-size container है जिसमें position, limit, और capacity होती है जो track करते हैं कि कितना लिखा या पढ़ा जा चुका है, और flip() call करने से यह filling mode से draining mode में बदल जाता है ताकि वही data वापस पढ़ा जा सके।

उदाहरण: Buffers

java
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());
	}
}

Channels

एक Channel किसी file या socket जैसे I/O source के एक खुले connection को represent करता है, और किसी stream के विपरीत, data एक channel के ज़रिए किसी Buffer में या उससे बाहर बहता है, अक्सर एक ही operation में बड़े blocks of data move करते हुए।

उदाहरण: Channels

java
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");
	}
}

Selectors

एक Selector एक single thread को data पढ़ने के लिए तैयार होने जैसे events के लिए एक साथ कई channels monitor करने देता है, जो वह तरीका है जिससे NIO per-connection एक thread की ज़रूरत के बिना हज़ारों connections handle करने देता है।

उदाहरण: Selectors

java
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());
	}
}

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

java
// 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");
	}
}
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. किसी buffer में लिखने के बाद और उससे पढ़ने से पहले buffer.flip() भूल जाना, जिससे आप गलत data पढ़ते हैं।
  2. किसी buffer को reuse करने से पहले clear() या compact() call न करना, जिससे अगली read के पास जगह नहीं होती या वह unread data को overwrite कर देती है।
  3. यह मान लेना कि एक read पूरा buffer भर देगा, जबकि यह कम bytes return कर सकता है, या stream के अंत में -1।
🔒

Chapter Quiz — Complete all 5 topics to unlock

0/5 topics done

Complete these topics first:

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.