How to write an image to file – Java ImageIO

The “javax.imageio.ImageIO” is a handy class to read and write image into local system. In this example, we show you how to use “ImageIO” to read an image from an URL and write it into three file formats :

  1. “jpg”
  2. “gif”
  3. “png”

Full example to demonstrate the ImageIO usage.

package com.mkyong.image;
import java.awt.image.BufferedImage;
import javax.imageio.ImageIO;
public class WriteImage 
    public static void main( String[] args )
    	BufferedImage image = null;
        try {
            URL url = new URL("");
            image =;
            ImageIO.write(image, "jpg",new File("C:\\out.jpg"));
            ImageIO.write(image, "gif",new File("C:\\out.gif"));
            ImageIO.write(image, "png",new File("C:\\out.png"));
        } catch (IOException e) {


Tags :

About the Author

Founder of and, love Java and open source stuff. Follow him on Twitter, or befriend him on Facebook or Google Plus. If you like my tutorials, consider make a donation to these charities.


  • ram


    but it is not woking for me. please help me, i am getting error shown in below.

    javax.imageio.IIOException: Can’t get input stream from URL!
    at Source)
    at Image.main(

    Caused by: Connection refused: connect
    at Method)
    at Source)
    at Source)

    thanks for your understand

  • Shinu Varghese

    How do you incorporate scribling of text onto an image in java…..

  • Shinu Varghese

    I have a java project in which i have to scribble text using a writing pad onto an existing image in a label…
    please help me on the above case……

  • http://www.univ_m' walid

    After converting the image to 0101 and encrypted, I can not bring it back as an image please help

  • Amit

    I am trying to upload a file path through action class to the server from local system.I have done it statically
    if(customerBean.getUploadFileFileName() != null)
    String filePath = “E:\\photos\\customer\\”;
    String photoPath = “E:/photos/customer/”+customerBean.getUploadFileFileName();

    File fileToCreate = new File(filePath, customerBean.getUploadFileFileName());
    setErrormessage(“Please check if Uploaded Photo exists in the Customer Folder”);
    return “failure”;
    status = custDAO.insertUser(customerBean,photoPath);

    Could you please help me out with a working code where i can upload images dynamically on the server and store the image path in server datatbase.


  • ramesh

    Respected sir,
    iam doing project on visual cryptography
    i.e: based on two images generate another image. here one image is orinal image with text. another is indecipheralble form. generate another image is also indecipherable form.both are combine whatever the text in original image. that will be visible. plz help me sir. plz

  • ramesh

    based on two images generate another image here one image is orinal image with text another is indecipheralble form generate another image is also indecipherable form. plz help me sir

  • ramesh

    sir plz help me iam doing project on visual cryptography.i want code in java

  • Fernando

    When i user an image *.png and try to get the *.bmp the file generated is a 0 bytes file.

    May be you could help me to know why is this happening??


  • tom costa

    Above code works on my machine , thanks for sharing

  • Jason

    Exception in thread “main” java.lang.IllegalArgumentException: im == null!

    Below is the code :

    BufferedImage bImageFromConvert = ByteArrayInputStream(binaryStream));		
    ImageIO.write(bImageFromConvert, "jpg", new File("D:\\"+ fileName +".jpg"));
    • swastik

      For Java its working, while I try to put the code for a managed bean in JSF in NetBean IDE the file is created under build\web directory instead of web. Can you please help on this

      FacesContext con = FacesContext.getCurrentInstance();
      ExternalContext contx = con.getExternalContext();
      String path = contx.getRealPath(“/”);
      URL url = new URL(“”);
      image =;

      ImageIO.write(image, “jpg”,new File(path +”out.jpg”));