Is there any built-in cross-platform way in Java
for parsing a Windows
path string and extracting the root path (including the drive letter) for Windows?
According to Java SE documentation, one can use the java.nio.file.Path
class, e.g.
String path = "C:\\this\\is\\a\\windows\\file path.txt"
Path p = Paths.get(path);
String drive = p.getRoot();
However, the Path
object construction is OS-specific, meaning if the above code will return a drive letter only if it is executed in Windows
; otherwise, it just returns null
.
Of course, one can use a custom approach (e.g., a regular expression or simple string processing) to retrieve the drive letter, if present, but is there any cross-platform way already implemented in Java for doing so?