Monday 19 February 2018

Difference between Java IO & NIO packages

(Download One Minute Java Mobile app for latest updates)

Overview
Java IO package provides API’s for input and output operations and Java NIO (Networking API) introduced in Java 7 provides an alternate way to do the same.

Comparison
Java IO Java NIO
Blocking I/O
Java IO's streams are blocking. Means when a thread invokes a read() or write(), that thread will wait until there is some data to read at source, or the data is fully written to destination
Non Blocking I/O
Java NIO's is non-blocking. Means a thread can issue a read request from a channel, and if nothing is available thread returns and can do some other task till the data is available for reading . It does not wait for the data to be read or write before returning.
Stream-oriented
Java IO API’s uses Streams for reading or writing data. Here a Java program uses stream to transport data from/to source/destincation.
Buffer-oriented
While writing data Java NIO API’s write it to buffer, and Channel reads data from buffer to write into destination.
While reading channel reads data from source and put it in buffer, and Java program using NIO package reads data from this buffer.
Selectors
Selectors are not available in Java IO
Selectors
Selectors are available in NIO package. Selectors are nothing but object that can monitor multiple channels at a time. To monitor a channel you need to first register it with selector and after that selector will observe the registered channels for 4 different operations i.e Connect, Accept, Read and Write.

No comments:

Post a Comment