44import java .awt .image .BufferedImage ;
55import java .io .ByteArrayOutputStream ;
66import java .io .IOException ;
7+ import java .nio .file .Files ;
78import java .nio .file .Path ;
89import java .nio .file .Paths ;
910import javax .imageio .ImageIO ;
@@ -37,7 +38,7 @@ public ExtractedImage(BufferedImage image, String filename, String saveFormat, i
3738 * Write the image to a file.
3839 * Uses the default image format and filename.
3940 *
40- * @param outputPath the output directory (must exist) .
41+ * @param outputPath the output path, it may be a file or a directory .
4142 * @throws IOException Throws if the file can't be accessed.
4243 */
4344 public void writeToFile (String outputPath ) throws IOException {
@@ -48,12 +49,14 @@ public void writeToFile(String outputPath) throws IOException {
4849 * Write the image to a file.
4950 * Uses the default image format and filename.
5051 *
51- * @param outputPath the output directory (must exist) .
52+ * @param outputPath the output path, it may be a file or a directory .
5253 * @throws IOException Throws if the file can't be accessed.
5354 */
5455 public void writeToFile (Path outputPath ) throws IOException {
55- var imagePath = outputPath .resolve (this .filename );
56- var outputfile = imagePath .toFile ();
56+ if (Files .isDirectory (outputPath )) {
57+ outputPath = outputPath .resolve (this .filename );
58+ }
59+ var outputfile = outputPath .toFile ();
5760 ImageIO .write (this .image , this .saveFormat , outputfile );
5861 }
5962
0 commit comments