有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

如何将图像写入文件–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) 个答案

  1. # 1 楼答案

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

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