Java: Get URI from FilePath
Solution 1
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.
Solution 2
Just use Normalize();
Example:
path = Paths.get("/", input).normalize();
this one line will normalize all your paths.
Solution 3
From SAXLocalNameCount.java from https://jaxp.java.net:
/**
* Convert from a filename to a file URL.
*/
private static String convertToFileURL ( String filename )
{
// On JDK 1.2 and later, simplify this to:
// "path = file.toURL().toString()".
String path = new File ( filename ).getAbsolutePath ();
if ( File.separatorChar != '/' )
{
path = path.replace ( File.separatorChar, '/' );
}
if ( !path.startsWith ( "/" ) )
{
path = "/" + path;
}
String retVal = "file:" + path;
return retVal;
}
Solution 4
The argument to new File(String)
is a path, not a URI. The part of your post after 'but' is therefore an invalid use of the API.
HarshG
Updated on January 21, 2020Comments
-
HarshG over 4 years
I've little knowledge of Java. I need to construct a string representation of an URI from
FilePath(String)
on windows. Sometimes theinputFilePath
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 withfile:/
, 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.
-
Thomas over 12 yearsI'd suggest using Apache Commons'
StringUtils.removeStart(...)
:brokenPath = StringUtils.removeStart(brokenPath, "file:/")
. -
HarshG over 12 yearsSo what should I do convert an URI to path? Essentially, to get a path which is not prefixed with "file:"
-
user207421 over 12 years@user1073005
new URI(uri).getPath()
, but this is a new question, isn't it? Your question above is about how to 'construct a string representation of an URI'. -
udoline about 7 yearssince JavaSE7 does that one line ...
java.nio.file.FileSystems.getDefault().getPath( xmlFileAsString ).toAbsolutePath().toUri()
Returns eg."file:///C:/develop/doku/projects/Documentry/THB/src/docbkx/Systemvoraussetzungen.xml"
-
Lii almost 6 years@udoline
Paths.get(xmlFileAsString)
is the correct method to use for that. It does the same thing internally. -
Gaurav almost 5 years@udoline is the .toAbsolutePath() method necessary before the call to .toUri()?
-
Gaurav almost 5 yearsWhich is better - 1. new File(inputFilePath).toURI() or 2. Paths.get(inputFilePath).toUri() ?
-
Gaurav almost 5 yearsWhich is better - 1. new File(inputFilePath).toURI() or 2. Paths.get(inputFilePath).toUri() ?
-
william.eyidi almost 5 years@gaurav in this case Paths.get() will provide a similar results but because we will not have to create a
FIle
object the second option might be better.