Java: what's the difference between NIO and NIO.2?

I don’t quite understand how different they are from each other, so I have several requests regarding these two packages.

Looking back a bit at Google, it looks like Oracle decided to upgrade the NIO package with the new and enhanced NIO.2 package as part of the JDK7 release.

  • How does the performance of the NIO package compare to the NIO.2 package?
  • What are the big changes from NIO to NIO.2 ? (for example, new methods, functions).
  • Why should the original NIO package be updated?
  • Is NIO.2 synonymous with the NIO package now?

Not that I wanted to use the old code in my code, I'm just really interested. Please tell me their differences?

+72
java io nio difference
Aug 27 '14 at 22:02
source share
3 answers

Initially, Java began with a proposal to the File class in the java.io package for accessing file systems. This object represents a file / directory and allowed you to perform some operations, such as checking if a file / directory exists, get properties and delete it. However, he had some flaws. To name a few:

  • The File class lacked some important functions, such as the copy method.
  • He also defined many methods that returned boolean . As you can imagine, in the case of an error, false was returned, not an exception. The developer really did not have the opportunity to find out why this failed.
  • Does not provide good handling supported by symbolic links.
  • A limited set of file attributes was provided.

To overcome these problems, the java.nio package was introduced in java 4. Key Features:

  • Channels and selectors. A channel is an abstraction of low-level file system functions, for example, mapped files.
  • Buffers: Buffering for all primitive classes (except logical).
  • Charset: Charset (java.nio.charset), encoders and decoders for matching bytes and Unicode characters

Java 7 introduces the java.nio.file package, which provides better support for handling symbolic links, accessing file attributes, and especially for supporting an extended file system using classes such as Path, Paths, and Files. You might want to take a look at the java.nio.file package description for more details on this.

With that in mind:

What are the big changes from NIO to NIO.2? (for example, new methods, features)?

They serve different purposes. To indicate big changes, you might want to take a look at the new java.nio.file package.

Why do I need to update the original NIO package?

This is not true. A new package was introduced, not updated.

Is NIO.2 just a synonym for the NIO package now? How to compare the performance of the NIO package with the NIO.2 package?

No, they are not synonyms. It also makes no sense to compare performance between the two, as they serve different purposes. NIO - A more abstract low-level input / output of data and NIO2, focused on file management.

Hope this helps.

[Bibliography: Oracle Certified Professional Java SE7 - A Comprehensive OCJP7 Certification Guide by S.G. Ganesh and Tushar Sharma - Chapter 9]

+79
Aug 28 '14 at 0:57
source share

NIO.2 introduced asynchronous i / o .

Asynchronous I / O is an approach to non-blocking I / O that is not supported by NIO.

NIO: reactor selector / circuit

NIO.2: completion handlers / proactor pattern

Thus, when Windows NIO.2 uses the I / O completion port , this should improve performance. In addition, no one knows, because no one uses Windows on the server side, and if they do, they will probably do it because they are heavily invested in .net, and for this reason, they most likely will not consider using Java

+46
May 27 '15 at 1:47
source share

My take:

Short version

This is the addition of the java.nio.file package with its significantly enhanced file and file system functionality.
In terms of network sockets or low-level file access, NIO == NIO.2 with some usability improvements.

Longer version

Java IO

Package: java.io
Old Blocking I / O API

Java NIO

Java 1.4 adds a new non-blocking API.
Package: java.nio
Java non-blocking I / O. Classes such as Selector , SelectorKey , Channel .
It seems to me that NIO was a big step forward for network I / O ( Selector , SelectorKey , SocketChannel , ServerSocketChannel , Buffer ), much less for file I / O ( FileChannel and Buffer only, including memory-mapped files). This is a fairly low-level API for both the network and file parts.

Java NIO.2

Added in Java 7. This is mainly the addition of a significantly improved API for file and file system management and addressing. The new file and file system API is at a relatively high level.

Package: java.nio.file and a few additions to the parent java.nio .
This is an add-on for file I / O and just a few minor additions to network I / O or a low-level file API.

The most notable low-level, optional file-related API add-ons are AsynchronousSocketChannel , AsynchronousServerSocketChannel and AsynchronousFileChannel , which add callback options for some methods. Asynchronous versions are mostly convenient; such display interfaces might have been hacked together even earlier, but they are now available in the JRE.

The new file API provides many useful functions - much more useful file system addressing with Path, significantly improved ZIP file processing using a custom file system provider, access to special file attributes, many convenient methods, such as reading the entire file with one command, copying the file using one command, etc. But all this is connected with the file / file system and everything is of a rather high level.

Repeating what I said above in terms of network sockets or low-level file access, NIO == NIO.2

Relevant Links

0
Sep 03 '19 at 19:51
source share



All Articles