Skip to content
Related Articles

Related Articles

Improve Article

Image Processing in Java | Set 1 (Read and Write)

  • Difficulty Level : Medium
  • Last Updated : 18 Nov, 2016

In this set we will learn how to read and write image file in Java.

Classes required to perform the operation:

  1. To read and write image file we have to import the File class [ import; ]. This class represents file and directory path names in general.
  2. To handle errors we use the IOException class [ import; ]
  3. To hold the image we create the BufferedImage object for that we use BufferedImage class [ import java.awt.image.BufferedImage; ]. This object is used to store an image in RAM.
  4. To perform the image read write operation we will import the ImageIO class [ import javax.imageio.ImageIO;]. This class has static methods to read and write an image.

// Java program to demonstrate read and write of image
import java.awt.image.BufferedImage;
import javax.imageio.ImageIO;
public class MyImage
    public static void main(String args[])throws IOException
        int width = 963;    //width of the image
        int height = 640;   //height of the image
        // For storing image in RAM
        BufferedImage image = null;
        // READ IMAGE
            File input_file = new File("G:\\Inp.jpg"); //image file path
            /* create an object of BufferedImage type and pass
               as parameter the width,  height and image int
               type.TYPE_INT_ARGB means that we are representing
               the Alpha, Red, Green and Blue component of the
               image pixel using 8 bit integer value. */
            image = new BufferedImage(width, height,
             // Reading input file
            image =;
            System.out.println("Reading complete.");
        catch(IOException e)
            System.out.println("Error: "+e);
        // WRITE IMAGE
            // Output file path
            File output_file = new File("G:\\Out.jpg");
            // Writing to file taking type and path as
            ImageIO.write(image, "jpg", output_file);
            System.out.println("Writing complete.");
        catch(IOException e)
            System.out.println("Error: "+e);
    }//main() ends here
}//class ends here

Note : This code will not run on online IDE as it needs an image on disk.

In the next set we will be learning how to get and set the pixel value of images in JAVA.

This article is contributed by Pratik Agarwal. If you like GeeksforGeeks and would like to contribute, you can also write an article using or mail your article to See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

My Personal Notes arrow_drop_up
Recommended Articles
Page :