2

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?

PNS
  • 19,295
  • 32
  • 96
  • 143
  • *"Is there any built-in cross-platform way in Java for parsing a Windows path string and extracting the drive letter?"* - No, as this kind of defeats the purpose of "cross-platform" – MadProgrammer Nov 11 '15 at 01:24
  • 2
    It most certainly does not and even the Java SE documentation for the `getRoot()` method implies otherwise. As a matter of fact, applications should be given the ability to handle paths from all operating systems in the same way, independently to the OS they run on. This is the definition of "cross-platform" and the Path class breaks this, contrary to its very own documentation. – PNS Nov 11 '15 at 18:58
  • *"As a matter of fact, applications should be given the ability to handle paths from all operating systems in the same way"* - Agreed, but a path is OS specific concept, what I mean is Windows is the only OS which has the concept of a drive letter, unix's root path is simply "/". If we're talking about mount points, then that's something entirely different, this is my point. I don't want to argue with you about it, you have a problem you want to solve which I don't fully understand, but seem to have found an otherwise working solution – MadProgrammer Nov 11 '15 at 21:34
  • So, I tried your code using a `path` of `/Users/{my home directory}` on MacOSX and `p.getRoot()` returns `/`. I took the same code and ran it on Windows and it returned `C:\`, so the API is doing exactly what it should be doing in a cross platform manner – MadProgrammer Nov 11 '15 at 21:53
  • What you describe about the return type of `p.getRoot()` is what I originally said in the question and it is platform-specific, returning a different value for the same input, depending on the platform. The problem is very simple: given a path string, get the root without needing to know if the path exists. This makes the platform irrelevant - we just want to analyze the path string, which is exactly what the `FilenameUtils` class in `Apache commons IO` does. It is a very realistic scenario, not worth downvoting. Anyway, there is a solution, so I will be using it. Thanks for the discussion. – PNS Nov 11 '15 at 22:25
  • Realistic but unusual. As a general rule a program only needs to manipulate file paths appropriate to the operating system it is running on, not for a specific operating system. That's why it isn't supported in the base library. – Harry Johnston Nov 11 '15 at 23:35
  • Not so unusual. One can easily come up with numerous use cases. For example, there are infinite amounts of log data out there that record file paths, which need to be processed as strings by an application running on another platform. – PNS Nov 12 '15 at 07:57

1 Answers1

1

There seems to be no built-in cross-platform way in Java for retrieving the root path (e.g., the drive) of a file, so a third-party library or custom code are the only solutions.

Apache Commons IO has a FilenameUtils class and its getPrefix() method does exactly what the question asked.

PNS
  • 19,295
  • 32
  • 96
  • 143