简体   繁体   中英

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?

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.

This class is a factory method to create Path objects which the intention is clearly indicated in the javadoc :

This class consists exclusively of static methods that return a Path by converting a path string or URI .

Which would be the goal to invoke this method if we already had a Path object


To resolve a resource Path from a base Path , you don't need to use again the Paths class.
Path methods provides operations on Path (comparison, Path creation between two Path s, etc..) as Path resolving.

Path resolve(Path other);

Resolve the given path against this path.

Or more simply said, it creates a Path from two of them.

Suppose a resource located in D:/my-resources/one-resource

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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