Load image from a filepath via BufferedImage
To read an .jpg file from non-relative path you could use this:
BufferedImage img = null;
try
{
img = ImageIO.read(new File("C:/ImageTest/pic2.jpg")); // eventually C:\\ImageTest\\pic2.jpg
}
catch (IOException e)
{
e.printStackTrace();
}
I do not have any Java environment at the moment, so hope it works and is written correctly.
getResource
& getResourceAsStream
do not work with file paths, but paths relative the code base. If the code base is C:
then a relative path that would locate the resource is /ImageTest/pic2.jpg
.
..difference between load file by
FileInputStream
andgetResourceAsStream
?
One major difference is that the getResource..
will work with a resource inside a Jar, which is no longer a File
. Therefore FileInputStream
cannot be used to access such a resource.