Java has stream oriented IO(java.io.) and Block oriented IO(java.nio.). How does block oriented IO improve the performance of IO?
Primarily by reducing the need for copying. Since the stream-oriented APIs have to move everything into managed Java variables, the system has to copy all of the data you deal with. When you use the NIO libraries, Java can directly map in the OS I/O pages without having to make copies (and deal with allocation and garbage collection).
The stream-based I/O uses streams to transfer data between a data source/sink and a Java program. The Java program reads from or writes to a stream a byte at a time. This approach to performing I/O operations is slow. The New Input/Ouput (NIO) solves the slow speed problem in the older stream-based I/O.

In NIO, you deal with channels and buffers for I/O operations.
A channel is like a stream. It represents a connection between a data source/sink and a Java program for data transfer.

There is one difference between a channel and a stream.
You can use a channel to read data as well as to write data. You can obtain a read-only channel, a write-only channel, or a read-write channel depending on your needs.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With