简体   繁体   English

java.io.File vs java.nio.Files这是新代码中的首选?

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

While writing answers around SO, a user tried pointing out that java.io.File should not be used in new code, instead he argues that the the new object java.nio.Files should be used instead; 在围绕SO编写答案时,用户试图指出java.io.File不应该在新代码中使用,而是他认为应该使用对象java.nio.Files ; he linked to this article . 他链接到这篇文章

Now I have been developing in Java for several years now, and have not heard this argument before; 现在我已经用Java开发了好几年了,之前没有听过这个论点; 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 File class will generally tell you exactly what the issue is. 自从阅读他一直在寻找的帖子,并没有找到许多证实这一点的其他消息来源,而且就个人而言,我觉得文章中提出的许多观点都很薄弱,如果你知道如何阅读它们,那么文件类通常会告诉您具体问题。

As I am continually developing new code my question is this: 当我不断开发新代码时,我的问题是:

Is this an active argument in the Java community? 这是Java社区中的一个活跃的参数吗? Is Files preferred over File for new code? 文件优于文件对新代码? What are the major advantages / disadvantages between the two? 两者之间的主要优点/缺点是什么?

The documentation that 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. java.nio.file包定义Java虚拟机的接口和类,以访问文件,文件属性和文件系统。 This API may be used to overcome many of the limitations of the java.io.File class. 此API可用于克服java.io.File类的许多限制。 The toPath method may be used to obtain a Path that uses the abstract path represented by a File object to locate a file. toPath方法可用于获取Path,该Path使用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. 生成的Path可以与Files类一起使用,以提供对其他文件操作,文件属性和I / O异常的更有效和广泛的访问,以帮助在文件操作失败时诊断错误。

File has a newer implementation: Path . File有一个更新的实现: Path With a builder Paths.get("...") . 使用构建器Paths.get("...") And Files has many nice utility functions with better implementations too ( move instead of the sometimes failing File.renameTo ). Files有很多很好的实用功能,也有更好的实现( move而不是有时失败的File.renameTo )。

A Path maintains its file system. Path维护其文件系统。 Hence you can copy out of a zip file system ("jar:file:..... .zip") some path to another file system and vice versa. 因此,您可以将zip文件系统(“jar:file:..... .zip”)复制到另一个文件系统的某个路径,反之亦然。

File.toPath() may help an incremental transition. File.toPath()可以帮助增量转换。

The utilities alone in Files make a move to the newer classes profitable. 仅在Files的实用程序就可以转移到可以获利的新类。

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.File or java.nio.Files meets the requirements better. 相反,我建议您查看应用程序所需的功能,并使用java.io.Filejava.nio.Files哪一个更好地满足要求。

The answer is likely to be either a draw, or java.nio.Files is better. 答案可能是平局,或java.nio.Files更好。 But it is up to you or your team to decide, not some other programmer on 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. 但我会说社区(一般而言)有更重要的问题。

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

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