简体   繁体   English

为什么java.nio.Paths.get(..)不将java.nio.Path对象作为输入?

[英]Why does java.nio.Paths.get(..) does not take java.nio.Path objects as input?

I started to use java.nio.* and now I am wondering: Why does java.nio.Paths.get(..) does not take java.nio.Path objects as input? 我开始使用java.nio.* ,现在我很想知道:为什么java.nio.Paths.get(..)不将java.nio.Path对象作为输入?

Now I all the time do something like this: 现在我一直在做这样的事情:

final Path basePath = Paths.get("/some/base/path");
final Path filePath = Paths.get(basePath.toString(), "file.txt");

So either I've overlooked something really fundamental or the API designer completely forgot about this option, what I strongly doubt. 因此,或者我忽略了一些真正的基本知识,或者API设计人员完全忘记了此选项,我对此深表怀疑。

This class is a factory method to create Path objects which the intention is clearly indicated in the javadoc : 此类是一种创建Path对象的工厂方法,其目的在javadoc中明确指出:

This class consists exclusively of static methods that return a Path by converting a path string or URI . 此类由静态方法组成,这些静态方法通过转换路径字符串或URI 返回Path

Which would be the goal to invoke this method if we already had a Path object 如果我们已经有一个Path对象,那将是调用此方法的目标


To resolve a resource Path from a base Path , you don't need to use again the Paths class. 要从基本Path解析资源Path ,则无需再次使用Paths类。
Path methods provides operations on Path (comparison, Path creation between two Path s, etc..) as Path resolving. Path的方法提供了关于操作Path (比较, Path创建之间的两个Path S,等。)作为Path解决。

Path resolve(Path other);

Resolve the given path against this path. 针对该路径解析给定路径。

Or more simply said, it creates a Path from two of them. 或更简单地说,它从其中两个创建Path

Suppose a resource located in D:/my-resources/one-resource 假设位于D:/my-resources/one-resource

Path relativePathResource = Paths.get("one-resource");
Path absolutePathResource = Paths.get("D:/my-resources").resolve(resource);

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

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