1

According to the API, the Java's ImageIO (javax.imageio.ImageIO) provides several overloaded methods for the .read() method.

Two of those methods were:

ImageIO.read(File input) 
ImageIO.read(URL input)

The Oracle tutorial website uses the read from file method ImageIO.read(File input). However I've seen many example coded by the programmers here prefer to use the URL approach ImageIO.read(URL input).

Exmaple:

img = ImageIO.read(new File("myImage.png"));
                        vs
img = ImageIO.read(getClass().getResource("images/myImage.png"));

My question is: If I am only coding for a Java Desktop app (and not a Java applet). Is there is significant advantage for using the URL approach over the other?


Note: There is a post with similar title in SO: Using URL or File (in ImageIO.read)

But this question address specifically on IDE. But I am not asking based on any specific IDE, but generally is there any prominent advantage for one over the other?

Community
  • 1
  • 1
user3437460
  • 17,253
  • 15
  • 58
  • 106

2 Answers2

2

..is there any prominent advantage for one over the other?

An URL can refer to a place on the internet, a file on the user's local file system, or a resource inside a Jar file - an .

A File can refer to a file on the user's local file system, and.. well, that is about it.

So, unless read/write access is required to the resource, I'd go with the URL since it is more versatile.

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • So the only advantage is versatility and maybe it is also safer to use the URL approach since it can also read embedded resources such as from the `JAR`. Am I right to say that? – user3437460 Jun 27 '15 at 19:19
  • *"So the only advantage is versatility.."* How many advantages do you *want?* One is enough to make me reach for the URL. *"..and maybe it is safer.."* I'd call it 'forward thinking'. After all, if the app. becomes distributed (typically as a Jar). The `File` will fail quite reliably. Exactly as reliably as the `URL` will work. So not so much 'safer' as 'not utterly broken'. – Andrew Thompson Jun 27 '15 at 19:23
  • Thanks a lot for your reply. Can you also explain your last sentence `So, unless read/write access is required to the resource, I'd go with the URL since it is more versatile` When write access is needed, how is `File` approach better than the `URL` approach then? – user3437460 Jun 27 '15 at 19:28
  • We can't open an output stream to an `URL`, but we [**can** to a `File`](https://docs.oracle.com/javase/8/docs/api/java/io/FileOutputStream.html).. If the code needs write access, `URL` is not an option. – Andrew Thompson Jun 27 '15 at 19:53
  • 1
    I like your answer, it was straight to the point and answered directly. – user3437460 Jun 27 '15 at 21:01
0

It is always better to keep your resources inside archive than keeping files in your file system. Suppose you want to distribute the JAR file. Then it will not work properly on other computers . Moreover one might delete those files by mistake . On the other hand JAR archives are meant to be uneditable . Better choice to keep inside JAR. If want to drop from internet , the story is different.

Rahul
  • 289
  • 2
  • 7