Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the exact use of java nio package when already methods are available with io package

I was learning java nio package and I realized there are lots of methods already provided by File which nio.Files is providing again by using Path class. Like that few more I got. I am actually not getting what is the actual use of nio package.

I am just very new to this package so may be my question is wrong but a little help can boost me to read further.

like image 522
Abhishek Choudhary Avatar asked Apr 29 '12 12:04

Abhishek Choudhary


People also ask

What is the use of NIO package in Java?

Java NIO is a buffer oriented package. It means that the data can be written/read to/from a buffer which further processed using a channel. Here, the buffers act as a container for the data as it holds the primitive data types and provides an overview of the other NIO packages.

What are channels and how are they utilized when using Java NIO?

Channels are the gateway provided by Java NIO to access the native I/O mechanism. We should use buffers to interact with the channels, so the channel is like a bridge between two entities to do the I/O. Buffers are the endpoints provided by channels to send and receive data.

What does Java NIO stand for?

nio (NIO stands for non-blocking I/O) is a collection of Java programming language APIs that offer features for intensive I/O operations. It was introduced with the J2SE 1.4 release of Java by Sun Microsystems to complement an existing standard I/O.

Which methods are found in the NIO 2 files class?

Java NIO Files class contains static methods that is used for manipulating files and directories and those methods mostly works on Path object.


2 Answers

The main difference between IO and NIO is that IO is blocking, while NIO is non-blocking.

This article explains the differences in the packages and what blocking and non-blocking IO is. archived

like image 159
Deco Avatar answered Sep 22 '22 11:09

Deco


Java programming, I/O has until recently been carried out using a stream metaphor. All I/O is viewed as the movement of single bytes, one at a time, through an object called a Stream. Stream I/O is used for contacting the outside world. It is also used internally, for turning objects into bytes and then back into objects.

NIO has the same role and purpose as original I/O, but it uses a different metaphor — block I/O. java.nio (new/non-blocking I/O) ) API was introduced with JDK1.4 .

What is difference between stream I/O and block I/O ?

A stream-oriented I/O system deals with data one byte at a time. An input stream produces one byte of data, and an output stream consumes one byte of data. It is very easy to create filters for streamed data. It is also relatively simply to chain several filters together so that each one does its part in what amounts to a single, sophisticated processing mechanism. On the flip side, stream-oriented I/O is often rather slow.

A block-oriented I/O system deals with data in blocks. Each operation produces or consumes a block of data in one step. Processing data by the block can be much faster than processing it by the (streamed) byte. But block-oriented I/O lacks some of the elegance and simplicity of stream-oriented I/O.

When you should use java.io and when should you prefer java.nio ?

  1. Scalability will probably drive your choice of package. java.net will require one thread per socket. Coding it will be significantly easier. java.nio is much more efficient, but is difficult to code around.

  2. You may get better scalability once you are dealing with tens of thousands of connections, but at lower numbers you’ll probably get better throughput with blocking IO.

  3. When working with SSL java.nio is not some thing easy to deal with

Important : If you are working with either of the packages, it is not a good idea to create the framework from scratch until and unless you have a compelling reason to do so.

For java.nio , the projects such as Grizzly and Quick Server provide reusable non blocking server components.

Worth reading Pain points with java.nio

Finally it boils down to specific requirements of your projects and what you are trying to achieve. Some of the best solutions may not require the most complex infrastructure at al

Update : Recently found out about NIO.2 package which exists since jdk 1.7. NIO.2 is different than NIO, the major being that NIO.2 offers asynchronous channel functionality . NIO.2 primer

If you are working with NIO, worth going through the difference and which one suits your purpose.

like image 43
vsingh Avatar answered Sep 22 '22 11:09

vsingh