Robert Lewis
Robert Lewis

Reputation: 1907

What exactly does it mean to "resolve" a path?

I've now read numerous articles on the use of Java somePath.resolve( someOtherPath ) but I can't find a precise definition, with helpful examples, of what exactly "resolving" a Path means. Everyone seems to assume you know. Can someone define it non-circularly? Or point me to a (non-circular) explainer?

Upvotes: 3

Views: 2447

Answers (2)

Jason
Jason

Reputation: 5246

The answer to this is defined in the documentation, which does a great job of indicating what the method does.

Converts a given path string to a Path and resolves it against this Path in exactly the manner specified by the resolve method. For example, suppose that the name separator is "/" and a path represents "foo/bar", then invoking this method with the path string "gus" will result in the Path "foo/bar/gus".

In other words, if the path C:/Program Files/Foo/ is the current Path and Bar exists in Foo, you could do the following:

Path parent = Paths.get("C:", "Program Files", "Foo");

Path child = parent.resolve("Bar");

Upvotes: 2

Atmas
Atmas

Reputation: 2393

Jason's answer is complete, but on the off chance you're looking for an even simpler answer/example..

I would say the "resolve" method that you specificaly referred to (which starts at somePath) starts at a certain Path, somePath, and then tells you what would happen if you choose to go to someOtherPath but specifically starting at somePath.

So if you had a directory structure on a PC, for example, like this:

C:
 - FolderA
   - Folder1
 - FolderB
   - Folder1

If you resolved someOtherPath as "Folder1", then it would depend where you started...

  • If your somePath was "/FolderA" then you'd end up at "C:/FolderA/Folder1"
  • If your somePath was "/FolderB" then you'd end up at "C:/FolderB/Folder1"
  • If your somePath was "/" (the root), then you'd have an invalid path...

Upvotes: 2

Related Questions