Java: Get URI from FilePath

前端 未结 5 927
被撕碎了的回忆
被撕碎了的回忆 2020-12-03 17:11

I\'ve little knowledge of Java. I need to construct a string representation of an URI from FilePath(String) on windows. Sometimes the inputFilePath

5条回答
  •  谎友^
    谎友^ (楼主)
    2020-12-03 17:48

    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;
    }
    

提交回复
热议问题