1

I have been looking on Google for a while now, and I just cant seem to find or remember how to access a file on the desktop with a URL. It's really frustrating me. Here is what I feel like it should be:

URL url = new URL("C:\\Users\\Austin\\Desktop\\hi.gif");

But it is just not working. I believe there is something that I should put instead of new URL but I cant find it. I've tried every search term I can think of.

How to turn file path as String into URL?

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
PulsePanda
  • 1,806
  • 10
  • 33
  • 56
  • file:/// should do the trick. See also: ---------- http://stackoverflow.com/questions/2166039/java-how-to-get-a-file-from-an-escaped-url – dctucker Nov 22 '12 at 03:04
  • 1
    @dctucker *"file:/// should do the trick."* That fails to produce a valid URL when the file name has spaces. Valid URLs should also only contain forward slash `/`. – Andrew Thompson Nov 22 '12 at 03:07

1 Answers1

3
// There are better ways to specify file paths, BNI.
File f = new File("C:\\Users\\Austin\\Desktop\\hi.gif");
URL url = f.toURI().toURL();
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433