java.io.File 与 java.nio.Files 哪个是新代码中的首选?
java.io.File vs java.nio.Files which is the preferred in new code?
在围绕 SO 编写答案时,用户试图指出 java.io.File
不应在新代码中使用,相反他认为 new 对象 java.nio.Files
应该改用;他链接到 this article.
现在我在Java开发了好几年了,以前没听过这种说法;自从阅读他的 post 我一直在搜索,并没有找到很多其他来源来证实这一点,就我个人而言,我觉得这篇文章中争论的许多观点都是薄弱的,如果你知道如何阅读它们, File class 抛出的错误通常会告诉您确切的问题所在。
由于我不断开发新代码,我的问题是:
这是 Java 社区中的一个活跃论点吗? Files preferred over File 是新代码吗?两者之间的主要优点/缺点是什么?
您链接的 documentation 给出了答案:
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.
File
有更新的实现:Path
。与生成器 Paths.get("...")
。而且 Files
有许多很好的实用函数,也有更好的实现(move
而不是有时会失败的 File.renameTo
)。
A Path
维护其文件系统。因此,您可以从 zip 文件系统 ("jar:file:..... .zip") 复制一些路径到另一个文件系统,反之亦然。
File.toPath()
可能有助于逐步过渡。
仅 Files
中的公用事业公司就转向更新的 类 盈利。
我会说 "preferred" 也不是。相反,我会建议您查看应用程序所需的功能,并使用 java.io.File
或 java.nio.Files
中哪个更符合要求。
答案很可能是平局,或者 java.nio.Files
更好。但这取决于您或您的团队,而不是 SO 上的其他程序员。
Is this an active argument in the Java community?
有些人什么事都会争论。但我想说社区(总体而言)有更重要的担忧。
在围绕 SO 编写答案时,用户试图指出 java.io.File
不应在新代码中使用,相反他认为 new 对象 java.nio.Files
应该改用;他链接到 this article.
现在我在Java开发了好几年了,以前没听过这种说法;自从阅读他的 post 我一直在搜索,并没有找到很多其他来源来证实这一点,就我个人而言,我觉得这篇文章中争论的许多观点都是薄弱的,如果你知道如何阅读它们, File class 抛出的错误通常会告诉您确切的问题所在。
由于我不断开发新代码,我的问题是:
这是 Java 社区中的一个活跃论点吗? Files preferred over File 是新代码吗?两者之间的主要优点/缺点是什么?
您链接的 documentation 给出了答案:
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.
File
有更新的实现:Path
。与生成器 Paths.get("...")
。而且 Files
有许多很好的实用函数,也有更好的实现(move
而不是有时会失败的 File.renameTo
)。
A Path
维护其文件系统。因此,您可以从 zip 文件系统 ("jar:file:..... .zip") 复制一些路径到另一个文件系统,反之亦然。
File.toPath()
可能有助于逐步过渡。
仅 Files
中的公用事业公司就转向更新的 类 盈利。
我会说 "preferred" 也不是。相反,我会建议您查看应用程序所需的功能,并使用 java.io.File
或 java.nio.Files
中哪个更符合要求。
答案很可能是平局,或者 java.nio.Files
更好。但这取决于您或您的团队,而不是 SO 上的其他程序员。
Is this an active argument in the Java community?
有些人什么事都会争论。但我想说社区(总体而言)有更重要的担忧。