have a look here for the full syntax: http://en.wikipedia.org/wiki/File_URI_scheme
for unix-like systems it will be as @Alex said file:///your/file/here
whereas for Windows systems would be file:///c|/path/to/file
File myFile=new File("/tmp/myfile");
URL myUrl = myFile.toURI().toURL();
Using Java 7:
Paths.get(string).toUri().toURL();
However, you probably want to get a URI
. Eg, a URI
begins with file:///
but a URL with file:/
(at least, that's what toString
produces).
You can also use
[AnyClass].class.getResource(filePath)
new File("path_to_file").toURI().toURL();
new URL("file:///your/file/here")
I tried it with Java on Linux. The following possibilities are OK:
file:///home/userId/aaaa.html
file:/home/userId/aaaa.html
file:aaaa.html (if current directory is /home/userId)
not working is:
file://aaaa.html
Source: Stackoverflow.com