This feels like it should be something straight forward, but I can seem to find an elegant solution to it without converting to File
.
Given a Path
Path path = Paths.get("/a/b/foo")
How to do get the path /a/b/foo.bar
? subpath
will return a relative path regardless of whether the original path is relative or absolute.
I would prefer not to have to use additional libraries. But, maybe that is the only way?
A path is either relative or absolute. An absolute path always contains the root element and the complete directory list required to locate the file. For example, /home/sally/statusReport is an absolute path. All of the information needed to locate the file is contained in the path string.
Technically in terms of Java, Path is an interface which is introduced in Java NIO file package during Java version 7,and is the representation of location in particular file system.As path interface is in Java NIO package so it get its qualified name as java. nio. file.
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");
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With