[英]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 我想知道
java.nio.file.Path
and java.nio.file.Paths
? java.nio.file.Path
和java.nio.file.Paths
? Path
and Paths
? Path
和Paths
? 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类的原因是多种因素的组合:
Path
is an interface, so direct calls of its constructor are not possible; Path
是一个接口,因此无法直接调用其构造函数。 as it has no constructor. 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()
可能是一种有效的面向对象的处理方式,但是旧代码阻止了添加而无需更多考虑。 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: 容易使其他人添加到库中的东西具有以下特征:
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. 如果他们采用将
String
和URI
修改为具有getPath(...)
函数的方法,那么他们将增加将代码集成到Java标准库中的难度。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.