Caleb Caleb - 3 months ago 22
Java Question

new File(path) or class.getResourses(path)?

BufferedImage image = ImageIO.read(SpriteSheet.class.getResource(path));
BufferedImage image = ImageIO.read(new File(path);


I've seen in couple of similar questions here before asking this, but I'm still confused a little.

I spent a bit of time reading java docs on those but it made the whole picture even more complex.

What do I need to use for uploading a resource in my program and what is the difference between the two?

Answer

SpriteSheet.class.getResource(path) finds the file that is in your java classpath.

new File(path) finds the file in anywhere on the file system

if your resource is in your classpath use class.getResource(path)

Comments