Read and Write Images to files in Java

While working with Images we often think images I/O is similar to any other File I/O, I recently faced a situation when I had to work with Images in Java.

There might be several other ways to do it, I liked it through ImageIO utility.

Read Image

   1: //read it from a file.
   2: BufferedImage fromFile = File("path to the file"));
   3: //read it from an InputStream
   4: Buffered fromIs = FileInputStream("path to the file"));
   5: //read it from a URL
   6: Buffered fromUrl = URL("a URL for the image"));

Write Image

   1: //Read the Image from a byte Array - I am assuming the Image coming from a stream

   2: BufferedImage image = ByteArrayOutputStream(bytearray));

   3: //Write the image to a file
   4: ImageIO.write(image,"GIF",new FileOutputStream("path to the file"));