Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java loading file from classpath vs file system, which is better?

Tags:

java

Lets say I want to load an image, "img.gif", would it be better to load it from the .JAR, or load it from the harddrive? Which is more efficient(speed, and memory usage), or are they essentially equal? I understand that loading from the classpath may be more convenient, but I'm disregarding that.

like image 579
Arman Avatar asked Jan 16 '15 03:01

Arman


2 Answers

Loading from a resource packed inside the jar is usually better because you can guarantee that the resource is always going to be there. Having the resource located somewhere else on the filesystem increases the chance that it could move or disappear.

Also having the resource inside the jar makes the jar more portable and available on different Operating Systems with different filesytems or on different machines with different folder structures.

I'm not sure if you would notice a speed difference eitherway.

like image 169
dkatzel Avatar answered Nov 15 '22 03:11

dkatzel


Smells of premature optimization, but a JAR file is a type of ZIP file that's written on a harddrive. Since reading the bytes off the harddrive directly avoids decompression it's probably faster. Of course, you should only need to read and decompress them once. Finally, images aren't typically compressible with LZW but jar provides additional advantages in terms of software deployment and maintenance.

like image 37
Elliott Frisch Avatar answered Nov 15 '22 04:11

Elliott Frisch