Learning Java IO and NIO

The major differences between Java IO and NIO

What is Java IO?

Java IO stands for Input/Output and it is the API within Java that allows for the reading and writing of data.

It is a part of the java.io package.

What is Java NIO?

Java NIO (Non-blocking IO) was introduced from Java version 1.4.

 

The following table explores the differences between Java IO and NIO.

Java IO

Java NIO

It is stream oriented

  •    Data is read as one or more byte at a time
  •        The data is not cached
  •       Cannot move back and forth in the data in a stream

It is buffer oriented

  •    Data is first fed into a buffer and then processed
  •    Can move back and forth through the data
  •      Buffer should contain all the data before it is processed
  •      Ensure that no data is overwritten when new data is read

Blocking IO

-          When the read() or write() is invoked by a thread, it is blocked until the data is completely read or written

Non-blocking IO

  •      The thread does not remain blocked, but when a request is made, it will return all the available data
  •          Similarly, with writing, when a request is made, a thread can write, but move on to something else, while waiting for the writing to be competed

There are no selectors

There are selectors

  •   A component with multiple NIO channels can be registered.
  •         It uses a single thread to “select” which channel is ready for reading or writing
  •      Allows a single thread to manage a no:of channels

(Jenkov, 2014) (JavaTPoint, n.d.)

 

References

JavaTPoint, n.d. Java IO vs. NIO. [Online]
Available at: https://www.javatpoint.com/java-nio-vs-input-output

Jenkov, J., 2014. Java NIO vs. IO. [Online]
Available at: http://tutorials.jenkov.com/java-nio/nio-vs-io.html

 

 

 


Comments

Popular Posts