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

5

52

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.

Undeniable answered 29/4, 2012 at 12:17 Comment(2)
Not an answser, so I'll post a comment: The Wikipedia page on it discusses some of the goals and reasons for the nio package.Kingship
Being pedantic, "Path" is part of the NIO2 library added in Java 7. BTW: While there is a lot of focus on non-blocking NIO sockets, I usually use blocking NIO Sockets which are simpler to use IMHO. NIO2 Add asynchronous NIO for Sockets and Files.Duenna
J
29

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 New website

Jaynajayne answered 29/4, 2012 at 12:20 Comment(4)
Please don't post just a link. When that link dies, so does this answer.Oceania
NIO can be blocking, non-blocking, or asynchronous. This is not the main difference at all.Illtimed
@Jaynajayne Blocking factor also holds good for java.io.File vs java.nio.file?Shutdown
Apart from the blocking. IO package performs operation using Streams where as NIO uses buffers. That is also the reason behind blocking behaviour.Tombola
A
25

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.

Antemortem answered 25/11, 2014 at 21:7 Comment(3)
I think this is the better answerCawthon
This answer is too generic for me to get anything out of it.Overpass
Frameworks like Spring Integration and netty provides non blocking.Deltoid
C
17

Java NIO: Channels and Buffers
In the standard IO API you work with byte streams and character streams. In NIO you work with channels and buffers. Data is always read from a channel into a buffer, or written from a buffer to a channel.

Java NIO: Non-blocking IO
Java NIO enables you to do non-blocking IO. For instance, a thread can ask a channel to read data into a buffer. While the channel reads data into the buffer, the thread can do something else. Once data is read into the buffer, the thread can then continue processing it. The same is true for writing data to channels.

Java NIO: Selectors
Java NIO contains the concept of "selectors". A selector is an object that can monitor multiple channels for events (like: connection opened, data arrived etc.). Thus, a single thread can monitor multiple channels for data.
More detail on orcale

Comeuppance answered 4/2, 2017 at 7:9 Comment(0)
O
2

Almost every method in java.io.File has issues that cannot be fixed for compatibility reasons, the most obvious is that the methods return a boolean when they fail. These issues plus the desire to support pluggable file systems and many other things necessitated the development of a completely new file system API so this is why java.nio.File was created.

Originality answered 30/4, 2012 at 13:1 Comment(0)
I
2

NIO also introduced Channels that abstract away the specialization in Stream - File, Socket, Network.

Insult answered 26/7, 2017 at 3:30 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.