public void write(BufferedImage bimg,String filePath) throws IOException {
  File file=new File(filePath);
  if (file.exists()) {
    if (!file.delete()) {
      throw new IOException("File " + filePath + " exists already, and call to .delete() failed "+ "unexpectedly");
    }
  }
 else {
    if (!file.createNewFile()) {
      throw new IOException("Unable to create file at path " + filePath + ", call to .createNewFile() "+ "failed unexpectedly.");
    }
  }
  OutputStream fos=new BufferedOutputStream(new FileOutputStream(file));
  try {
    write(bimg,fos);
  }
  finally {
    try {
      fos.close();
    }
 catch (    IOException e) {
    }
  }
}

public void write ( BufferedImage bimg , String filePath ) throws IOException { File file = new File ( filePath ) ; if ( file . exists ( ) ) { if ( ! file . delete ( ) ) { throw new IOException ( "File " + filePath + " exists already, and call to .delete() failed " + "unexpectedly" ) ; } } else { if ( ! file . createNewFile ( ) ) { throw new IOException ( "Unable to create file at path " + filePath + ", call to .createNewFile() " + "failed unexpectedly." ) ; } } OutputStream fos = new BufferedOutputStream ( new FileOutputStream ( file ) ) ; try { write ( bimg , fos ) ; } finally { try { fos . close ( ) ; } catch ( IOException e ) { } } }
writes the image out to the target file , creating the file if necessary , or overwriting if it already exists .
