Java:NIO和NIO.2之间究竟有什么区别? [英] Java: what exactly is the difference between NIO and NIO.2?

查看:145
本文介绍了Java:NIO和NIO.2之间究竟有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不太明白他们之间有多么不同,所以我对这两个套餐有一些疑问。

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

在谷歌上看了一下后,它似乎Oracle决定使用更新且增强的 NIO.2 软件包更新 NIO 软件包,作为JDK7版本的一部分。

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. NIO 包的性能与<$ c $相比如何? c> NIO.2 包裹?

  2. NIO 到<$ c $有什么重大变化C> NIO.2 ? (例如新方法,功能)

  3. 为什么原始 NIO 包必须更新?

  4. NIO.2 现在只是 NIO 套餐的同义词吗?

  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?

推荐答案

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

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:


  • File类缺少一些重要的功能,例如复制方法。

  • 它还定义了许多返回 boolean 的方法。可以想象,如果出现错误,则返回 false ,而不是抛出异常。事实上,开发人员无法知道失败的原因。

  • 没有提供支持符号链接的良好处理。

  • 有限集提供了文件属性。

  • 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.

为了克服这些问题,在java 4中引入了java.nio包。主要功能包括:

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


  • 频道和选择器:频道是较低级别文件系统功能的抽象,例如内存映射文件。

  • 缓冲区:缓冲所有基本类(布尔除外)。

  • Charset:Charset(java.nio.charset) ,编码器和解码器映射字节和Unicode符号

使用java 7引入java.nio.file包提供更好的支持用于处理符号链接,文件属性访问,特别是通过路径,路径和文件等类来支持扩展文件系统。你可能想看看 java.nio.file包描述以获取更多详细信息。

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.

考虑到这一点:


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

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

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

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


为什么原始的NIO包必须更新?

Why did the original NIO package have to be updated?

它没有。引入了一个新的包而不是更新。

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


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

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

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

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.

希望这会有所帮助。

[参考书目:Oracle认证专业Java SE7 - 全面的OCJP7认证指南,SGGanesh和Tushar Sharma的
- 第9章]

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

这篇关于Java:NIO和NIO.2之间究竟有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆