简体   繁体   中英

How to write an image to file – Java ImageIO

I would like to write an image to a file using Java ImageIO. This is my piece of code:

Document doc = convertDocument (fileName, "grey");                       
      ImageIO.write(Converter.convertSVGToPNG(doc), "png",
              new File(FOLDER_PNG_OUT_GREY + fileName.replaceAll(".svg", ".png")));

But I get this error when running my code:

java.io.FileNotFoundException: C:\\Work\\eclipse-tdk\\svgManager\\svgManager\\src\\main\\resources\\icons\\svg\\grey_png\\$pac.png (The system cannot find the path specified)

I also tried:

String pngFileName = FOLDER_PNG_OUT_GREY + fileName.replaceAll(".svg", ".png");
File outputfile = new File(pngFileName);
bImage = ImageIO.read(outputfile);

but then I got the error: Can't read input file!

我建议使用getResource(String file)方法获取图像资源,如下所示:

File initialImage = new File(YourClass.class.getResource("pac.png").getFile());

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM