jar*_*bjo 268
new File(path).toURI().toURL();
Run Code Online (Sandbox Code Playgroud)
Ale*_*sky 36
使用Java 7:
Paths.get(string).toUri().toURL();
Run Code Online (Sandbox Code Playgroud)
但是,你可能想得到一个URI.例如,a URI开头file:///只有一个URL file:/(至少是toString产生的东西).
Ale*_*lex 20
new URL("file:///your/file/here")
Run Code Online (Sandbox Code Playgroud)
File myFile=new File("/tmp/myfile");
URL myUrl = myFile.toURI().toURL();
Run Code Online (Sandbox Code Playgroud)
有关完整的语法,请看这里:http : //en.wikipedia.org/wiki/File_URI_scheme(
对于类似Unix的系统)将像@Alex所说的,file:///your/file/here而对于Windows系统将file:///c|/path/to/file