title: Replace existing file using FileImageOutputStream
body: <p>I'm trying to replace a file after changing it's quality - the problem is I'm only able to create a new file, not replace the original. Here is the code I'm using:</p>
  
  <pre><code>private static void resize(String folder, String filename)
          throws IOException {
  
      BufferedImage imagem = ImageIO.read(new File(folder + "/" + filename));
  
      float quality = (float) 0.25;
  
      ImageWriter writer = (ImageWriter) ImageIO.getImageWritersByFormatName(
              "jpeg").next();
      ImageWriteParam iwp = writer.getDefaultWriteParam();
      iwp.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
      iwp.setCompressionQuality(quality);
      File file = new File(folder + "/" + filename);
      FileImageOutputStream output = new FileImageOutputStream(file);
      writer.setOutput(output);
      IIOImage image = new IIOImage(imagem, null, null);
      writer.write(null, image, iwp);
      writer.dispose();
  }
  </code></pre>