如何将图像写入文件–Java ImageIO
我想使用JavaImageIO将图像写入文件。这是我的一段代码:
Document doc = convertDocument (fileName, "grey");
ImageIO.write(Converter.convertSVGToPNG(doc), "png",
new File(FOLDER_PNG_OUT_GREY + fileName.replaceAll(".svg", ".png")));
但我在运行代码时遇到以下错误:
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)
我还尝试:
String pngFileName = FOLDER_PNG_OUT_GREY + fileName.replaceAll(".svg", ".png");
File outputfile = new File(pngFileName);
bImage = ImageIO.read(outputfile);
但后来我发现了一个错误: 无法读取输入文件强>
# 1 楼答案
我建议使用
getResource(String file)
方法获取图像资源,如下所示: