简体   繁体   English

Java中IO和NIO的路径

[英]Path and paths of IO and NIO in java

I'm new at learning about IO and NIO in Java. 我是新来学习Java中的IO和NIO的人。

And I wonder what 我想知道

  • is the difference between java.nio.file.Path and java.nio.file.Paths ? 是的区别java.nio.file.Pathjava.nio.file.Paths
  • is the difference between Path and Paths ? 是的区别PathPaths

Thank for any explaining. 感谢您的解释。

Path is a class that represents a path. 路径是代表路径的类。 Anything where you typically already have a Path involves the methods located here. 通常您已经拥有Path任何地方都涉及此处的方法。

Paths is a set of utilities. 路径是一组实用程序。 These utilities produce Path objects from other types of input. 这些实用程序从其他类型的输入生成Path对象。 The utilities do not require having a Path ahead of time. 该实用程序不需要事先具有Path They are convenience wrappers for common, often repeatedly used code, to reduce the need to cut-and-paste. 它们是常见的,经常重复使用的代码的便捷包装,以减少剪切和粘贴的需要。

Here is an example of using Paths : 这是使用Paths的示例:

/* I have a String, but need a Path */
Path path = Paths.get("/home/user/.config");

Here is an example of using Path : 这是使用Path的示例:

/* I have a Path, but need a String */
String name = path.toString();

The reason why a Utility class like Paths is required is a combination of a number of factors: 之所以需要诸如Paths类的Utility类的原因是多种因素的组合:

  1. Path is an interface, so direct calls of its constructor are not possible; Path是一个接口,因此无法直接调用其构造函数。 as it has no constructor. 因为它没有构造函数。
  2. String is a final class, and a class where modifying it would probably be far more difficult to present as an alternative than creating a utility class. String是一个最终类,而在其中进行修改的类可能比创建一个实用程序类要困难得多。 So while "/home/user/.config".toPath() might be a valid object-oriented way of doing things, legacy code prevents adding that without more deliberation. 因此,尽管"/home/user/.config".toPath()可能是一种有效的面向对象的处理方式,但是旧代码阻止了添加而无需更多考虑。
  3. URI has pressures on it similar to the pressures described above on String . URI对它的压力类似于上面在String上描述的压力。

When they added in the NIO Path class, they wanted their code to be reviewed and integrated into the core Java library. 当他们添加到NIO Path类中时,他们希望对其代码进行检查并将其集成到核心Java库中。

Stuff that is easy to get others to add to a library has the following characteristics: 容易使其他人添加到库中的东西具有以下特征:

  1. You don't mess with existing parts of the library in ways that are exposed by the established library call interface (doing this disrupts users of the library, as now they need to rewrite their programs). 您不会以已建立的库调用接口公开的方式来处理库的现有部分(这样做会打乱库的用户,因为现在他们需要重写程序)。
  2. You have a backout plan that is easy (this is critical because you might not be able to deliver on time) 您有一个简单的退出计划(这很关键,因为您可能无法按时交付)

If they took the approach of modifying String and URI to have a getPath(...) function, then they would have increased the difficulty in getting their code integrated into the Java standard library. 如果他们采用将StringURI修改为具有getPath(...)函数的方法,那么他们将增加将代码集成到Java标准库中的难度。

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

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