How to Read and Write Image Files in Java


In this example we will show how to write an image to file in Java.

Source Code

package com.beginner.examples;

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;

import javax.imageio.ImageIO;

public class WriteImage {

	public static void main(String[] args) {
		BufferedImage image = null;
		try {
			URL imageURL = new URL("https://gss0.bdstatic.com/70cFsjip0QIZ8tyhnq/img/logo-zhidao.gif");
			image = ImageIO.read(imageURL);
			
			ImageIO.write(image, "jpg",new File("E:tmptestImage.jpg"));
            ImageIO.write(image, "gif",new File("E:tmptestImage.gif"));
            ImageIO.write(image, "png",new File("E:tmptestImage.png"));
		}catch(MalformedURLException e) {
			e.printStackTrace();
		}catch(IOException e) {
			e.printStackTrace();
		}
	}
}

References

Imported packages in Java documentation:

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments