Java: Get URI from FilePath

76,768

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.

Share:
76,768
HarshG
Author by

HarshG

Updated on January 21, 2020

Comments

  • HarshG
    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 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.

  • Thomas
    Thomas over 12 years
    I'd suggest using Apache Commons' StringUtils.removeStart(...): brokenPath = StringUtils.removeStart(brokenPath, "file:/").
  • HarshG
    HarshG over 12 years
    So what should I do convert an URI to path? Essentially, to get a path which is not prefixed with "file:"
  • user207421
    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
    udoline about 7 years
    since 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/‌​Systemvoraussetzunge‌​n.xml"
  • Lii
    Lii almost 6 years
    @udoline Paths.get(xmlFileAsString) is the correct method to use for that. It does the same thing internally.
  • Gaurav
    Gaurav almost 5 years
    @udoline is the .toAbsolutePath() method necessary before the call to .toUri()?
  • Gaurav
    Gaurav almost 5 years
    Which is better - 1. new File(inputFilePath).toURI() or 2. Paths.get(inputFilePath).toUri() ?
  • Gaurav
    Gaurav almost 5 years
    Which is better - 1. new File(inputFilePath).toURI() or 2. Paths.get(inputFilePath).toUri() ?
  • william.eyidi
    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.