java examples
Home Java Examples Resources Java Interview Questions
Brainpower SEO

How to Read Image File?

Here you see a code sample to read an image file. This code will work either the image file is located in a file folder or inside a jar file. You can use javax.imageio.ImageIO class to read the image file.

import java.awt.image.BufferedImage;
import java.io.IOException;
import javax.imageio.ImageIO;

public class ReadingImage
{
    public void getImage()
    {
        try
        {
            BufferedImage image = 				 
                ImageIO.read(getClass().getResource("Sunset.jpg"));

            // Do something with the image.
        } catch (IOException e)
        {
            e.printStackTrace();
        }
    }

    public static void main(String[] args)
    {		
        ReadingImage ri = new ReadingImage();
        ri.getImage();
    }
}