java.io.File 与 java.nio.Files 哪个是新代码中的首选?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32143633/
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-11-02 19:43:48  来源:igfitidea点击:

java.io.File vs java.nio.Files which is the preferred in new code?

javafileioniojava-io

提问by Matt Clark

While writing answers around SO, a user tried pointing out that java.io.Fileshould not be used in new code, instead he argues that the the newobject java.nio.Filesshould be used instead; he linked to this article.

在围绕 SO 编写答案时,用户试图指出java.io.File不应在新代码中使用,而是认为应该使用对象java.nio.Files;他链接到这篇文章

Now I have been developing in Java for several years now, and have not heard this argument before; since reading his post I have been searching, and have not found many other sources that confirm this, and personally, I feel like many of the points argued in the article are weak and that if you know how to read them, errors thrown by the Fileclass will generally tell you exactly what the issue is.

现在我已经用Java开发好几年了,以前没有听过这种说法;自从阅读他的帖子以来,我一直在搜索,并没有找到许多其他来源证实这一点,而且就我个人而言,我觉得文章中争论的许多观点都很薄弱,如果你知道如何阅读它们,则抛出的错误文件类通常会准确地告诉您问题是什么。

As I am continually developing new code my question is this:

当我不断开发新代码时,我的问题是:

Is this an active argument in the Java community? Is Filespreferred over Filefor new code? What are the major advantages / disadvantages between the two?

这是 Java 社区中的积极争论吗?是文件优于文件对新代码?两者之间的主要优点/缺点是什么?

采纳答案by Jens

The documentationthat you linked give the answer:

您链接的文档给出了答案:

The java.nio.file package defines interfaces and classes for the Java virtual machine to access files, file attributes, and file systems. This API may be used to overcome many of the limitations of the java.io.File class. The toPath method may be used to obtain a Path that uses the abstract path represented by a File object to locate a file. The resulting Path may be used with the Files class to provide more efficient and extensive access to additional file operations, file attributes, and I/O exceptions to help diagnose errors when an operation on a file fails.

java.nio.file 包定义了 Java 虚拟机访问文件、文件属性和文件系统的接口和类。此 API 可用于克服 java.io.File 类的许多限制。toPath 方法可用于获取 Path,该 Path 使用 File 对象表示的抽象路径来定位文件。生成的 Path 可与 Files 类一起使用,以提供对其他文件操作、文件属性和 I/O 异常的更有效和广泛的访问,以帮助在文件操作失败时诊断错误。

回答by Joop Eggen

Filehas a newer implementation: Path. With a builder Paths.get("..."). And Fileshas many nice utility functions with better implementations too (moveinstead of the sometimes failing File.renameTo).

File有一个更新的实现:Path. 与建设者Paths.get("...")。并且Files具有许多具有更好实现的不错的实用程序函数(move而不是有时会失败File.renameTo)。

A Pathmaintains its file system. Hence you can copy out of a zip file system ("jar:file:..... .zip") some path to another file system and vice versa.

APath维护其文件系统。因此,您可以从 zip 文件系统(“jar:file:..... .zip”)中复制到另一个文件系统的某些路径,反之亦然。

File.toPath()may help an incremental transition.

File.toPath()可能有助于渐进式过渡。

The utilities alone in Filesmake a move to the newer classes profitable.

仅公用事业公司就Files可以盈利。

回答by Stephen C

I would say that neither is "preferred". Instead, I would recommend that you look at the functionality your application requires and use which of java.io.Fileor java.nio.Filesmeets the requirements better.

我会说两者都不是“首选”。相反,我建议你看看你的应用程序需要的功能和使用其中的java.io.Filejava.nio.Files符合要求更好。

The answer is likely to be either a draw, or java.nio.Filesis better. But it is up to you or your team to decide, not some other programmer on SO.

答案很可能是平局,或者java.nio.Files更好。但这取决于您或您的团队来决定,而不是 SO 上的其他一些程序员。



Is this an active argument in the Java community?

这是 Java 社区中的积极争论吗?

There are some people who will argue about anything. But I would say that the community (in general) has more important concerns.

有些人会为任何事情争论。但我想说的是,社区(总的来说)有更重要的问题。