197

How do I create a new URL object using a local file, for the purpose of unit tests?

Nathan
  • 8,093
  • 8
  • 50
  • 76
MeanwhileInHell
  • 6,780
  • 17
  • 57
  • 106

8 Answers8

326
new File(path).toURI().toURL();
jarnbjo
  • 33,923
  • 7
  • 70
  • 94
53

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

Aleksandr Dubinsky
  • 22,436
  • 15
  • 82
  • 99
  • "...a URI begins with file:/// but a URL with file:/ ..." Is that the case for both Windows and Linux? – ptntialunrlsd Jul 13 '15 at 09:19
  • @ptntialunrlsd That is a good question. I haven't checked, but I would guess yes. – Aleksandr Dubinsky Jul 13 '15 at 15:26
  • 10
    No. An URL is just a special case of an URI. A file URI starts with "file://" and then lists the host (generally omitted), followed by "/" and the path "foo/bar" (generally meant to be read as an absolute path). Thus "file:///foo/var". An URI that looks like "file:/foo/bar" is incorrect. See also: [file URI scheme](https://en.wikipedia.org/wiki/File_URI_scheme) – David Tonhofer Sep 02 '15 at 14:00
  • @DavidTonhofer Thank you for the explanation of URIs, but that doesn't answer **ptntialunrlsd**'s question. What does '...toURL().toString()' produce on Linux? Also, I've reverted your edits because they made my answer more wordy without changing the meaning. – Aleksandr Dubinsky Sep 05 '15 at 17:16
  • 3
    @AleksandrDubinsky It's best to leave pointers to the Oracle javadoc in though.. easier to click through to `java.nio.file.Paths`. Also, please be sure to make clear that you mean the _implementations_ in "URI vs URL". Anway `java.net.URL.toString()` produces the same thing on Unix, as it must. It only displays one "/" which is _very wrong_ (see [file URI scheme](https://en.wikipedia.org/wiki/File_URI_scheme)). I guess this is in Java because of reasons, better use `java.net.URI`. It correctly generates "file://[host]/" on a call to `.toString()`. – David Tonhofer Sep 05 '15 at 21:20
  • @DavidTonhofer Actually, URIs (RFC 3986, RFC 8089) allow the single-slash `file:/path` form, but URLs (RFC 1738) don't. – Aleksandr Dubinsky Nov 19 '21 at 07:48
43
new File("path_to_file").toURI().toURL();
Ted Hopp
  • 232,168
  • 48
  • 399
  • 521
23
new URL("file:///your/file/here")
Alex
  • 32,506
  • 16
  • 106
  • 171
10
File myFile=new File("/tmp/myfile");
URL myUrl = myFile.toURI().toURL();
Sean Patrick Floyd
  • 292,901
  • 67
  • 465
  • 588
5

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

Liv
  • 6,006
  • 1
  • 22
  • 29
  • 7
    Don't do that manually. `File.toURI().toURL()`is the way to go – Sean Patrick Floyd May 23 '11 at 14:25
  • 3
    @SeanPatrickFloyd sometimes you don't have a choice, like when it is in a `.properties` file. – Sled Jan 30 '14 at 19:24
  • @ArtB I don't see how that makes a difference – Sean Patrick Floyd Jan 31 '14 at 08:04
  • 2
    @SeanPatrickFloyd, this question/answer comes up when you search for `java file url`, which in my case means that I was searching for the format of a `file://` URL, in Java, for use in a `.properties` file, or to type in manually, etc. – daveloyall Apr 28 '15 at 21:40
  • 1
    @SeanPatrickFloyd sometimes you don't have access to the source code, just to the property, and `file://` is unfortunately necessary. Being system dependent is not such a huge issue since it's a mutable property. – vikingsteve Mar 20 '17 at 11:57
4

You can also use

[AnyClass].class.getResource(filePath)
xMichal
  • 624
  • 2
  • 7
  • 19
0

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
SzB
  • 1,027
  • 10
  • 12