Java: what exactly is the difference between NIO and NIO.2?

John Huynh picture John Huynh · Aug 28, 2014 · Viewed 32.4k times · Source

I don't quite understand how different they are from each other so I have some inquiries regarding these two packages.

After looking around a bit on Google, it seems like Oracle decided to update the NIO package with the newer and enhanced NIO.2 package as part of the JDK7 release.

  1. How does the performance of NIO package compare with the NIO.2 package?
  2. What are some big changes from NIO to NIO.2? (e.g. new methods, features)
  3. Why did the original NIO package have to be updated?
  4. Is NIO.2 just synonymous with the NIO package nowadays?

It's not that I want to use the legacy package in my code, I'm just really curious about this. Please tell me their differences?

Answer

Sousa Gaspar picture Sousa Gaspar · Aug 28, 2014

Java started initially by offering the File class, in the java.io package to access file systems. This object represents a file/directory and did allow you to perform some operations such as checking if a file/directory exists, get properties and delete it. It had, though, some shortcomings. To name a few:

  • The File class lacked some important functionality, such as a copy method.
  • It also defined many methods that returned boolean. As one can imagine, in case of an error, false was returned, rather than throwing an exception. The developer had, indeed, no way of knowing why it failed.
  • Did not provide good handling on support of symbolic links.
  • A limited set of file attributes was provided.

To overcome these problems, java.nio package was introduced in java 4. The key features were:

  • Channels and Selectors: A channel is an abstraction on lower-level file system features, e.g. memory-mapped files.
  • Buffers: Buffering for all primitive classes (except for Boolean).
  • Charset: Charset (java.nio.charset), encoders, and decoders to map bytes and Unicode symbols

With java 7 the java.nio.file package is introduced providing a better support for handling symbolic links, file attributes access and specially to support extended the file system through classes such as Path, Paths and Files. You might wanna to have a look at the java.nio.file package description to get further details on this.

With this in mind:

What are some big changes from NIO to NIO.2? (e.g. new methods, features)?

They serve different purposes. To point out big changes you might want to look at the all new package java.nio.file.

Why did the original NIO package have to be updated?

It didn't. A new package was introduced rather than updated.

Is NIO.2 just synonymous with the NIO package nowadays? How does the performance of NIO package compare with the NIO.2 package?

No, they are not synonymous. It also does not make much sense to compare performance between them, as they serve different purposes. NIO a more abstract low level data I/O 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]