简体   繁体   English

Java:NIO和NIO.2的区别究竟是什么?

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

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. 在谷歌上看一下之后,似乎Oracle决定使用更新的增强型NIO.2软件包更新NIO软件包,作为JDK7版本的一部分。

  1. How does the performance of NIO package compare with the NIO.2 package? NIO包的性能与NIO.2包相比如何?
  2. What are some big changes from NIO to NIO.2 ? NIONIO.2哪些重大变化? (eg new methods, features) (例如新方法,功能)
  3. Why did the original NIO package have to be updated? 为什么原始的NIO包必须更新?
  4. Is NIO.2 just synonymous with the NIO package nowadays? 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? 请告诉我他们的不同之处?

Java started initially by offering the File class, in the java.io package to access file systems. Java最初是通过在java.io包中提供File类来访问文件系统而开始的。 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. File类缺少一些重要的功能,例如复制方法。
  • It also defined many methods that returned boolean . 它还定义了许多返回boolean方法。 As one can imagine, in case of an error, false was returned, rather than throwing an exception. 可以想象,如果出现错误,则返回false ,而不是抛出异常。 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: 为了克服这些问题,在java 4中引入了java.nio包。主要功能包括:

  • Channels and Selectors: A channel is an abstraction on lower-level file system features, eg 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:Charset(java.nio.charset),编码器和解码器,用于映射字节和Unicode符号

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. 使用java 7,引入了java.nio.file包,为处理符号链接,文件属性访问提供了更好的支持,并特别支持通过Path,Paths和Files等类扩展文件系统。 You might wanna to have a look at the java.nio.file package description to get further details on this. 您可能希望查看java.nio.file包描述以获取有关此内容的更多详细信息。

With this in mind: 考虑到这一点:

What are some big changes from NIO to NIO.2? 从NIO到NIO.2有哪些重大变化? (eg 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 . 要指出大的更改,您可能需要查看全新的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? NIO.2现在只是NIO套件的代名词吗? How does the performance of NIO package compare with the NIO.2 package? 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 SGGanesh and Tushar Sharma - Chapter 9] [参考书目:Oracle认证专业Java SE7 - 由SGGanesh和Tushar Sharma撰写的全面的OCJP7认证指南 - 第9章]

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. 因此,在Windows上, NIO.2使用I / O完成端口 ,这将提高性能。 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,如果他们这样做,他们可能会这样做,因为他们大量投资于.net,因此很可能不会考虑使用Java。

My take: 我的看法:

Short version 精简版

It's the addition of java.nio.file package with its high level much enhanced file and file system functionality. 它是java.nio.file包的添加,具有高级别的增强文件和文件系统功能。
From the perspective of network sockets or low level file access, NIO == NIO.2 with a few convenience improvements. 从网络套接字或低级文件访问的角度来看,NIO == NIO.2有一些方便的改进。

Longer version 更长的版本

Java IO Java IO

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

Java NIO Java NIO

Added in Java 1.4, the new non-blocking API. 在Java 1.4中添加了新的非阻塞API。
Package: java.nio 包: java.nio
Java non-blocking IO. Java非阻塞IO。 Classes like Selector , SelectorKey , Channel . SelectorSelectorKeyChannel这样的类。
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 ( FileChannel and Buffer only, including memory mapped files). 在我看来,NIO是网络I / O( SelectorSelectorKeySocketChannelServerSocketChannelBuffer )的一大进步,更不用说文件I / O(仅限FileChannelBuffer ,包括内存映射文件)。 This is a fairly low level API, for both network and file parts. 对于网络和文件部分,这是一个相当低级别的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. 在Java 7中添加。它主要是关于添加大量改进的文件和文件系统操作以及寻址API。 The new file and filesystem related API is relatively high level. 新文件和文件系统相关的API相对较高。

Package: java.nio.file and few additions to parent java.nio . 包: java.nio.file和父java.nio一些补充。
This additions are for file I/O and only few minor additions to network I/O or low level file API. 这些新增功能适用于文件I / O,只有少量添加到网络I / O或低级文件API。

Most notable low-level, not necessary file related, API additions are AsynchronousSocketChannel , AsynchronousServerSocketChannel and AsynchronousFileChannel , which adds callbacks variants to some methods. 最值得注意的低级,不必要的文件相关的API添加是AsynchronousSocketChannelAsynchronousServerSocketChannelAsynchronousFileChannel ,它们为某些方法添加了回调变体。 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. 这样的映射接口甚至可能在之前已被黑客攻击,但现在它们在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 相关链接

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM