I've little knowledge of Java. I need to construct a string representation of an URI from FilePath(String)
on windows. Sometimes the inputFilePath
I get is: file:/C:/a.txt
and sometimes it is: C:/a.txt
. Right now, what I'm doing is:
new File(inputFilePath).toURI().toURL().toExternalForm()
The above works fine for paths, which are not prefixed with file:/
, but for paths prefixed with file:/
, the .toURI
method is converting it to a invalid URI, by appending value of current dir, and hence the path becomes invalid.
Please help me out by suggesting a correct way to get the proper URI for both kind of paths.
These are the valid file uri:
file:/C:/a.txt <- On Windows
file:///C:/a.txt <- On Windows
file:///home/user/a.txt <- On Linux
So you will need to remove file:/
or file:///
for Windows and file://
for Linux.