Java:NIO 和 NIO.2 到底有什么区别?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25537675/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 00:35:45  来源:igfitidea点击:

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

javaioniodifference

提问by John Huynh

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 NIOpackage with the newer and enhanced NIO.2package as part of the JDK7 release.

在 Google 上环顾了一下之后,似乎 Oracle 决定使用更新的NIO增强NIO.2包更新该包,作为 JDK7 版本的一部分。

  1. How does the performance of NIOpackage compare with the NIO.2package?
  2. What are some big changes from NIOto NIO.2? (e.g. new methods, features)
  3. Why did the original NIOpackage have to be updated?
  4. Is NIO.2just synonymous with the NIOpackage nowadays?
  1. NIO封装的性能与封装相比如何NIO.2
  2. NIO到 有哪些重大变化NIO.2?(例如新方法、新功能)
  3. 为什么NIO必须更新原始包?
  4. 现在NIO.2只是NIO包的代名词吗?

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?

并不是我想在我的代码中使用遗留包,我只是对此很好奇。请告诉我它们的区别?

采纳答案by Sousa Gaspar

Java started initially by offering the Fileclass, in the java.iopackage 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:

Java 最初通过提供File类,在java.io包中访问文件系统。该对象代表一个文件/目录,并允许您执行一些操作,例如检查文件/目录是否存在、获取属性并删除它。但是,它有一些缺点。仅举几例:

  • 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, falsewas 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.
  • File 类缺少一些重要的功能,例如复制方法。
  • 它还定义了许多返回boolean. 可以想象,如果发生错误,false则返回,而不是抛出异常。事实上,开发人员无法知道它为什么会失败。
  • 没有提供对符号链接支持的良好处理。
  • 提供了一组有限的文件属性。

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

为了克服这些问题,java 4 中引入了 java.nio 包。主要特点是:

  • 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
  • 通道和选择器:通道是对低级文件系统功能的抽象,例如内存映射文件。
  • 缓冲区:缓冲所有原始类(布尔值除外)。
  • Charset:用于映射字节和 Unicode 符号的字符集 (java.nio.charset)、编码器和解码器

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 descriptionto get further details on this.

在 java 7 中引入了 java.nio.file 包,为处理符号链接、文件属性访问提供更好的支持,并特别支持通过 Path、Paths 和 Files 等类扩展文件系统。您可能想查看java.nio.file 包描述以获取有关此内容的更多详细信息。

With this in mind:

考虑到这一点:

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

从 NIO 到 NIO.2 有哪些大的变化?(例如新方法、功能)?

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

它们用于不同的目的。要指出重大变化,您可能需要查看全新的包java.nio.file

Why did the original NIO package have to be updated?

为什么要更新原来的NIO包?

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?

现在 NIO.2 只是 NIO 包的代名词吗?NIO 包的性能与 NIO.2 包相比如何?

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.

不,它们不是同义词。比较它们之间的性能也没有多大意义,因为它们用于不同的目的。NIO 是一个更抽象的低级数据 I/O,而 NIO2 则专注于文件管理。

Hope this helps.

希望这可以帮助。

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

[参考书目:Oracle 认证专家 Java SE7 - 综合 OCJP7 认证指南,SGGanesh 和 Tushar Sharma - 第 9 章]

回答by Eugene Beresovsky

NIO.2 introduced asynchronous i/o.

NIO.2 引入了异步 i/o

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

异步 I/O 是一种 NIO 不支持的非阻塞 I/O 方法。

NIO: selectors / reactor pattern

NIO:选择器/反应器模式

NIO.2: completion handlers / proactor pattern

NIO.2:完成处理程序/前摄器模式

Thus when on Windows, NIO.2 uses I/O Completion Ports, which should boost performance. Except, nobody knows, because nobody uses Windows on the server side, and if they do, they probably do so because they are heavily invested in .net, and for that reason will most likely not consider using Java.

因此,在 Windows 上,NIO.2 使用 I/O Completion Ports,这应该会提高性能。除了,没有人知道,因为没有人在服务器端使用 Windows,如果他们这样做,他们可能会这样做,因为他们在 .net 上投入了大量资金,因此很可能不会考虑使用 Java。

回答by Espinosa

My take:

我的看法:

Short version

精简版

It's the addition of java.nio.file package with its high level much enhanced file and file system functionality.
From the perspective of network sockets or low level file access, NIO == NIO.2 with a few convenience improvements.

它是 java.nio.file 包的添加,其高级别的增强的文件和文件系统功能。
从网络套接字或低级文件访问的角度来看,NIO == NIO.2 有一些便利性改进。

Longer version

更长的版本

Java IO

Java IO

Package: java.io
Old blocking I/O API

包:java.io
旧的阻塞 I/O API

Java NIO

蔚来

Added in Java 1.4, the new non-blocking API.
Package: java.nio
Java non-blocking IO. Classes like Selector, SelectorKey, Channel.
It seems to me the NIO was a big step up for network I/O (Selector, SelectorKey, SocketChannel, ServerSocketChannel, Buffer), much less for file I/O (FileChanneland Bufferonly, including memory mapped files). This is a fairly low level API, for both network and file parts.

在 Java 1.4 中添加了新的非阻塞 API。
包:java.nio
Java 非阻塞 IO。类如Selector, SelectorKey, Channel
在我看来,NIO 是网络 I/O ( Selector, SelectorKey, SocketChannel, ServerSocketChannel, Buffer) 的一大进步,更不用说文件 I/O(FileChannel并且Buffer仅包括内存映射文件)。这是一个相当低级的 API,用于网络和文件部分。

Java NIO.2

Java NIO.2

Added in Java 7. It's mainly about addition of much improved file and filesystem manipulation and addressing API. The new file and filesystem related API is relatively high level.

在 Java 7 中添加。它主要是关于添加大量改进的文件和文件系统操作和寻址 API。新的文件和文件系统相关的 API 是比较高级的。

Package: java.nio.fileand few additions to parent java.nio.
This additions are for file I/O and only few minor additions to network I/O or low level file API.

Package:java.nio.file以及对 parent 的一些补充java.nio
这些添加用于文件 I/O,并且仅对网络 I/O 或低级文件 API 进行了少量添加。

Most notable low-level, not necessary file related, API additions are AsynchronousSocketChannel, AsynchronousServerSocketChanneland AsynchronousFileChannel, which adds callbacks variants to some methods. The Asynchronous versions of are mainly a convenience addition; such mapping interfaces could have been hacked together even before, but now they are available out-of-the box in the JRE.

最显着的低级、与文件无关的 API 新增功能是AsynchronousSocketChannel,AsynchronousServerSocketChannelAsynchronousFileChannel,它们为某些方法添加了回调变体。的异步版本主要是一个方便的补充;这样的映射接口甚至在以前就可以被破解,但现在它们在 JRE 中开箱即用。

The new file API brings a lots of goodies - much more useful file system addressing with Path, much improved ZIP file manipulation using custom file system provider, special file attributes access, lots of convenience methods like reading whole file with one command, copying file with one command etc. But it's all file/filesystem related and all quite high level.

新的文件 API 带来了很多好处 - 使用 Path 更有用的文件系统寻址,使用自定义文件系统提供程序大大改进了 ZIP 文件操作,特殊文件属性访问,许多方便的方法,例如使用一个命令读取整个文件,使用一个命令等。但它与所有文件/文件系统相关,并且都非常高。

Reiterating what I have already said above, from the perspective of network sockets or low level file access, NIO == NIO.2

重申我上面已经说过的,从网络套接字或低级文件访问的角度来看,NIO == NIO.2

Relevant links

相关链接