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

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

This is useful where a file name needs to be replaced with another file name.

Using this method ensures that the result Path object is for the same FileSystem as the source Path object.

So, to add extension ".bar" to a Path:

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

Leave a Comment