简体   繁体   English

Java:使用ImageIO写入图像文件

[英]java: writing an image file with ImageIO

My code looks as so: 我的代码如下所示:

    File saveLocation = new File("src\\database\\images\\");
    ImageIO.write(this.image, "jpg", saveLocation);

And my error is this: 我的错误是这样的:
java.io.FileNotFoundException: src\\database\\images (Access is denied) java.io.FileNotFoundException:src \\ database \\ images(访问被拒绝)

images is a folder in my netbeans project images是我的netbeans项目中的一个文件夹
I can read from it just fine. 我可以从中读懂。

Two things 两件事情

  1. src\\\\database\\\\images\\\\ is likely a directory, you need to supply a file name for the image, something like src\\\\database\\\\images\\\\Output.jpg for example src\\\\database\\\\images\\\\可能是目录,您需要提供图像的文件名,例如src\\\\database\\\\images\\\\Output.jpg
  2. You shouldn't be referencing the src directory, ever. 您永远都不应引用src目录。 Instead, you need to save the file somewhere the file system. 相反,您需要将文件保存在文件系统中的某个位置。 You could use ./Output.jpg for the current working directory (which is typically the project directory) 您可以将./Output.jpg用于当前的工作目录(通常是项目目录)

Ummm. That's a directory... how could you read from it just fine? 那是一个目录...您怎么能从中读取呢? All I notice is that you are missing a file name. 我注意到的是您缺少文件名。

 File saveLocation = new File("src/database/images/myImage.jpg"); // <-- Use `/`.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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