Java imageio.IIOException:无法读取输入文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18309868/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
imageio.IIOException: Can't read input file
提问by trolologuy
I've started Java a week ago, and now I would like to insert an image into my window. Whatever I try I keep having this in Eclipse: javax.imageio.IIOException: Can't read input file!
我一周前开始使用 Java,现在我想在我的窗口中插入一个图像。无论我尝试什么,我都会在 Eclipse 中保持这个状态: javax.imageio.IIOException:无法读取输入文件!
package graphics;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JPanel;
import src.Common;
public class Window extends JFrame
{
public class Panel extends JPanel
{
public void paintComponent(Graphics g)
{
Image img;
try
{
img = ImageIO.read(new File("/logo.jpg"));
g.drawImage(img, 0, 0, this);
}
catch (IOException e)
{
e.printStackTrace();
}
}
}
public Window(String title, int width, int height)
{
this.setTitle(title);
this.setSize(width, height);
this.setLocationRelativeTo(null);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setContentPane(new Panel());
this.setVisible(true);
}
}
}
I think the code is pretty self-explaining. I tried to solve the problem with this, this, and that.
我认为代码是不言自明的。我试图用这个、这个和那个来解决这个问题。
What I'm trying to do is a desktop program, and my sources are stored like that : training/src/graphics/Window training/src/src/main
我想做的是一个桌面程序,我的源代码是这样存储的:training/src/graphics/Window training/src/src/main
I did put the image I want to read in every folder, and still getting the issue :/
我确实把我想阅读的图像放在每个文件夹中,但仍然出现问题:/
What did I do wrong?
我做错了什么?
EDIT Finally solved, here the answer
编辑终于解决了,这里是答案
nIcE cOw gave me the linkthat helped. So I did put my images into a folder, and change the way to access to them, as described in the link.
好牛给了我帮助的链接。因此,我确实将我的图像放入了一个文件夹中,并更改了访问它们的方式,如链接中所述。
getClass().getResource("/images/yourImageName.extension");
采纳答案by deepthought-64
Have you tried using new File("logo.jpg");
(without the leading /)?
您是否尝试过使用new File("logo.jpg");
(不带前导 /)?
And are you sure, the logo.jpg is copied to your output? (Some IDEs don't copy every file from your source-directories to your output (or target) directories.)
您确定将 logo.jpg 复制到您的输出中吗?(某些 IDE 不会将源目录中的每个文件都复制到输出(或目标)目录中。)
/src
|-> Window.java
|-> Logo.jpg
becomes
变成
/out
|-> Window.class
(Note that the IDE/compiler does not copy the image to your output-directory and so the compiled code cannot find the image - allthough you did specify the correct path)
(请注意,IDE/编译器不会将图像复制到您的输出目录,因此编译后的代码无法找到该图像 - 尽管您确实指定了正确的路径)
回答by LastFreeNickname
Try do debug which file resource you actually try to access. First step would be to get your new File("/logo.jpg").get [Canonical]Path()
and print it to System.out
(or alternatively watch in the the debugger). I guess the problem is the /
before logo.jpg
, which points to your root directory (e.g. c:) and your file isn't there, but I don't know your file setup in detail.
尝试调试您实际尝试访问的文件资源。第一步是获取new File("/logo.jpg").get [Canonical]Path()
并将其打印到System.out
(或在调试器中观看)。我猜问题是/
before logo.jpg
,它指向您的根目录(例如 c:),而您的文件不存在,但我不知道您的文件设置的详细信息。
回答by Mrbull32
The problem is that you're looking at nothing before the image, so it's looking into a folder that isn't there to find it.
问题是你在图像之前什么都没有看,所以它正在寻找一个不存在的文件夹来找到它。
You have to create a folder to store the images in your project, and then call to it, your folder name in front of the image name. e.g.
您必须创建一个文件夹来存储项目中的图像,然后调用它,将您的文件夹名称放在图像名称之前。例如
ImageIO.read(new File("Folder/Image.png"));
Otherwise you can find the image by going through the entire directory, which isn't a good way as it takes longer, and when you move your project it won't be a working link as the directory will be different. For example:
否则,您可以通过遍历整个目录来找到图像,这不是一个好方法,因为它需要更长的时间,并且当您移动项目时,它不会是一个工作链接,因为目录会有所不同。例如:
ImageIO.read(new File("D:/eclipse/Workspace/Project/Folder/Image.png"));
Creating a folder in your project so its on the same level as the source folder in the directory and call to it for the image, like so:
在您的项目中创建一个文件夹,使其与目录中的源文件夹处于同一级别,并为图像调用它,如下所示:
Folder structure;
settings
src
Image Folder
bin
文件夹结构;
设置
源文件
图片文件夹
垃圾桶