code: public static boolean writeImage(BufferedImage image, String filename,
              int dpi, float quality) throws IOException
      {
          File file = new File(filename);
          FileOutputStream output = new FileOutputStream(file);
          try
          {
              String formatName = filename.substring(filename.lastIndexOf('.') + 1);
              return writeImage(image, formatName, output, dpi, quality);
          }
          finally
          {
              output.close();
          }
      }
docs: Writes a buffered image to a file using the given image format.
  See {@link #writeImage(BufferedImage image, String formatName,
  OutputStream output, int dpi, float quality)} for more details.
  
  @param image the image to be written
  @param filename used to construct the filename for the individual image. Its suffix will be
  used as the image format.
  @param dpi the resolution in dpi (dots per inch) to be used in metadata
  @param quality quality to be used when compressing the image (0 &lt; quality &lt; 1.0f)
  @return true if the image file was produced, false if there was an error.
  @throws IOException if an I/O error occurs