Java I/O (Input/Output) সিস্টেমে দুটি প্রধান পদ্ধতি রয়েছে: Traditional I/O (Blocking I/O) এবং NIO (New I/O)। Java NIO (New I/O) Java 1.4 থেকে চালু হয়েছিল এবং এটি Traditional I/O এর তুলনায় আরও আধুনিক এবং দ্রুত I/O অপারেশন প্রদান করে।
এখানে Traditional I/O এবং NIO এর মধ্যে কিছু গুরুত্বপূর্ণ পার্থক্য আলোচনা করা হয়েছে:
1. Architecture:
| বিষয় | Traditional I/O (Blocking I/O) | NIO (New I/O) |
|---|---|---|
| Design | সাধারণত Stream-based I/O। | Buffer-based I/O (Data is read/written into buffers). |
| Threading | I/O অপারেশন থ্রেড ব্লক করে, যা থ্রেডকে এক্সিকিউট করতে বাধা দেয়। | Non-blocking I/O সাপোর্ট করে, একাধিক অপারেশন একই থ্রেডে একসাথে পরিচালনা করা যায়। |
| Blocking I/O | Yes, ডেটা পড়া বা লেখা থ্রেডকে ব্লক করে রাখে। | No, Non-blocking I/O। |
2. Performance:
| বিষয় | Traditional I/O (Blocking I/O) | NIO (New I/O) |
|---|---|---|
| Performance | অনেক সময় থ্রেড ব্লক হয়ে যাওয়ায় I/O অপারেশন ধীর হতে পারে। | NIO প্যারালাল অপারেশন সাপোর্ট করে, যা দ্রুত I/O অপারেশন নিশ্চিত করে। |
| Concurrency | একটি থ্রেড একে একে অপারেশন করতে বাধ্য হয়। | Multiple channels can be managed concurrently in a non-blocking fashion. |
| Efficiency | থ্রেডের জন্য বেশি রিসোর্স প্রয়োজন, যা ইফিসিয়েন্সি কমাতে পারে। | NIO কম রিসোর্স ব্যবহার করে, এবং একাধিক I/O অপারেশন পরিচালনা করতে পারে। |
3. Memory Management:
| বিষয় | Traditional I/O (Blocking I/O) | NIO (New I/O) |
|---|---|---|
| Buffering | Typically uses a byte/character stream with minimal buffering. | NIO uses Buffers to store data before reading/writing. |
| Memory Allocation | Stream-based I/O may need to allocate memory dynamically. | Memory-mapped buffers provide more control over memory allocation. |
4. Flexibility and Scalability:
| বিষয় | Traditional I/O (Blocking I/O) | NIO (New I/O) |
|---|---|---|
| Flexibility | Less flexible, mainly focuses on one I/O stream at a time. | More flexible, handles multiple channels, files, and non-blocking I/O efficiently. |
| Scalability | Less scalable for handling a large number of I/O operations concurrently. | NIO is highly scalable, supports multiplexing and asynchronous I/O. |
5. Key Components:
| বিষয় | Traditional I/O (Blocking I/O) | NIO (New I/O) |
|---|---|---|
| Main Components | Streams (InputStream, OutputStream, Reader, Writer) | Buffers, Channels, Selectors |
| Buffer | Not used directly in Traditional I/O. | Buffers are central for NIO, storing data for reading/writing. |
| Channels | Not present in Traditional I/O. | Channels handle the I/O operations and interact with buffers. |
| Selectors | Not available in Traditional I/O. | Selectors allow non-blocking I/O multiplexing (e.g., handling multiple channels). |
6. File Handling:
| বিষয় | Traditional I/O (Blocking I/O) | NIO (New I/O) |
|---|---|---|
| File Handling | Uses FileInputStream and FileOutputStream to read/write files. | Uses FileChannel, Files, and **FileInputStream/FileOutputStream for enhanced file operations. |
| File Attributes | Limited file attribute handling. | NIO supports advanced file operations (e.g., Files, Paths, FileChannel). |
Key Differences Between Traditional I/O and NIO:
| Aspect | Traditional I/O (Blocking I/O) | NIO (New I/O) |
|---|---|---|
| Stream vs. Buffer | Based on streams (character or byte-based). | Based on buffers (data is transferred in buffers). |
| Blocking vs. Non-blocking | Blocks the thread while waiting for I/O operations. | Non-blocking, allows asynchronous operations. |
| Thread Management | Each I/O operation requires a dedicated thread. | Multiple I/O operations can be handled in a single thread (with selectors). |
| Concurrency | Less efficient for high-concurrency systems. | Better suited for scalable, high-concurrency systems. |
| I/O Model | Simple but not efficient for high loads. | Efficient for handling large numbers of I/O operations. |
Example Code:
Traditional I/O (Blocking I/O):
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
public class TraditionalIOExample {
public static void main(String[] args) throws IOException {
FileInputStream fis = new FileInputStream("source.txt");
FileOutputStream fos = new FileOutputStream("destination.txt");
int byteData;
while ((byteData = fis.read()) != -1) {
fos.write(byteData);
}
fis.close();
fos.close();
System.out.println("File copied using Traditional I/O");
}
}
NIO Example with FileChannel:
import java.nio.file.*;
import java.nio.channels.*;
import java.io.IOException;
public class NIOExample {
public static void main(String[] args) throws IOException {
Path source = Paths.get("source.txt");
Path destination = Paths.get("destination.txt");
try (FileChannel sourceChannel = FileChannel.open(source, StandardOpenOption.READ);
FileChannel destinationChannel = FileChannel.open(destination, StandardOpenOption.WRITE, StandardOpenOption.CREATE)) {
sourceChannel.transferTo(0, sourceChannel.size(), destinationChannel);
System.out.println("File copied using NIO");
}
}
}
- Traditional I/O (Blocking I/O) সহজ এবং ব্যবহারকারী বান্ধব হলেও এটি উচ্চ পরিমাণ ডেটা বা একাধিক I/O অপারেশন পরিচালনা করার জন্য দক্ষ নয়।
- NIO (New I/O) অধিক কার্যকরী এবং বহুমুখী, যেখানে বড় পরিমাণ ডেটা প্রসেস এবং একাধিক I/O অপারেশন পরিচালনা করা যায়, এবং এটি non-blocking I/O সাপোর্ট করে।
NIO আধুনিক high-performance systems এবং concurrent applications-এর জন্য আদর্শ, কারণ এটি কম রিসোর্সে বেশি কার্যকারিতা প্রদান করে এবং প্যারালাল অপারেশন সহজ করে তোলে।
Content added By
Read more