200

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

9 Answers 9

336
new File(path).toURI().toURL();
1
  • 30
    For java 7+: Paths.get("path","to","stuff").toUri().toURL()
    – Ajax
    Commented Nov 19, 2015 at 0:32
57

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 🤷

6
  • "...a URI begins with file:/// but a URL with file:/ ..." Is that the case for both Windows and Linux? Commented Jul 13, 2015 at 9:19
  • @ptntialunrlsd That is a good question. I haven't checked, but I would guess yes. Commented Jul 13, 2015 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 Commented Sep 2, 2015 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. Commented Sep 5, 2015 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). I guess this is in Java because of reasons, better use java.net.URI. It correctly generates "file://[host]/" on a call to .toString(). Commented Sep 5, 2015 at 21:20
43
new File("path_to_file").toURI().toURL();
23
new URL("file:///your/file/here")
5
  • 1
    where /your/file/here is an absolute path to a file on Unix/Linux. On Windows it would be different I think. Commented May 23, 2011 at 14:23
  • 7
    That's not very clever, since you have to handle the escaping of characters which are not allowed in URLs yourself. On Windows (and potentially other operating systems), you also have to modify the path separator from the native path to the file.
    – jarnbjo
    Commented May 23, 2011 at 14:25
  • 1
    new URL("file:my.properties");
    – weberjn
    Commented Oct 4, 2017 at 14:03
  • While this is correct, it is not portable as it depends on absolute paths.
    – mazunki
    Commented May 2, 2020 at 14:59
  • 2
    On Windows the following worked for me: file:///C:\\file.zip Commented Apr 15, 2021 at 8:36
10
File myFile=new File("/tmp/myfile");
URL myUrl = myFile.toURI().toURL();
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

5
  • 7
    Don't do that manually. File.toURI().toURL()is the way to go Commented May 23, 2011 at 14:25
  • 3
    @SeanPatrickFloyd sometimes you don't have a choice, like when it is in a .properties file.
    – Sled
    Commented Jan 30, 2014 at 19:24
  • @ArtB I don't see how that makes a difference Commented Jan 31, 2014 at 8: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
    Commented Apr 28, 2015 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. Commented Mar 20, 2017 at 11:57
4

You can also use

[AnyClass].class.getResource(filePath)
2
  • 3
    but only if that file exists within the classpath
    – aepurniet
    Commented Feb 6, 2014 at 21:20
  • 2
    If the "filePath" can be found in a jar, the resulting URL is like jar:file:/home/user/a/b/c/foo.jar!/com/example/stuff/config.txt. Commented Sep 2, 2015 at 14:39
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
0

For Windows: You have use in this way if file is in some drive:

eg.:

Actual link:

"C:\Users\er2\Downloads\Designer3.png"

Converted link:

"file:/C:/Users/er2/Downloads/Designer3.png"

Upvote if worked for you!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.