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

若如初见. 提交于 2019-11-27 10:16:46

问题


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.


回答1:


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.




回答2:


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.




回答3:


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




回答4:


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




回答5:


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.



来源:https://stackoverflow.com/questions/10372066/what-is-the-exact-use-of-java-nio-package-when-already-methods-are-available-wit

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!