Java nio: How to add an extension to an absolute path?

It seems like it should be something direct, but I can find an elegant solution without converting it to File.

Considering Path

Path path = Paths.get("/a/b/foo")

How to make a way /a/b/foo.bar? subpathreturns a relative path regardless of whether the original path is relative or absolute.

I would prefer not to use additional libraries. But maybe this is the only way?

+4
source share
1 answer

To change the file name Path, use one of the methods resolveSibling():

This is useful when you need to replace the file name with a different file name.

, Path FileSystem Path.

, ".bar" Path:

path = path.resolveSibling(path.getFileName() + ".bar");
+11

Source: https://habr.com/ru/post/1612678/


All Articles