Pass a local file in to URL in Java
new File(path).toURI().toURL();
Using Java 11:
Path.of(string).toUri();
Using Java 7:
Paths.get(string).toUri();
To convert to the old-school URL class (why?), add .toURL()
. Note there is a difference in the string output. The modern URI::toString
begins with file:///
(the traditional URL syntax) while the nearly-deprecated URL::toString
with file:/
(the modern URI syntax). Weird 🤷
new File("path_to_file").toURI().toURL();