IntelliJ javax.imageio.IIOException:无法读取输入文件

问题描述:

我只是想要加载图像。IntelliJ javax.imageio.IIOException:无法读取输入文件

在一个较旧的程序中,它的工作原理。唯一的区别是,旧的使用目录,这个使用包。其实我甚至不能创建一个目录。

那栈跟踪:

javax.imageio.IIOException: Can't read input file! 
    at javax.imageio.ImageIO.read(ImageIO.java:1301) 
    at com.company.ImageLoader.loadImage(ImageLoader.java:17) 
    at com.company.Window.Window.<init>(Window.java:30) 
    at com.company.Engine.<init>(Engine.java:21) 
    at com.company.Main.main(Main.java:8) 

enter image description here

enter image description here

public class Window 
{ 
    private JFrame myFrame; 
    private JPanel mainPanel; 
    private JLabel mainLabel; 
    private ImageLoader myImageLoader = new ImageLoader(); 

    public Window(Boolean defaultLaFDeco, String title, int x, int y, int width, int height) 
    { 
     JFrame.setDefaultLookAndFeelDecorated(defaultLaFDeco); 
     myFrame = new JFrame(); 
     myFrame.setTitle(title); 
     mainPanel = new JPanel(); 
     mainLabel = new JLabel(); 
     mainPanel.add(mainLabel); 
     myFrame.add(mainPanel); 
     myFrame.setBounds(x,y,width,height); 
     myFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); 
     myFrame.setVisible(true); 

     File file = new File("Res/cat/0.jpg"); 
     BufferedImage img = myImageLoader.loadImage(file); 
    } 
} 

public class ImageLoader 
{ 
    private BufferedImage image = null; 

    public BufferedImage loadImage(File file) 
    { 
     BufferedImage img = null; 
     try 
     { 
      img = ImageIO.read(file); 
     } catch (IOException e) 
     { 
      e.printStackTrace(); 
     } 
     return img; 
    } 
} 
+0

您不能在包树中创建目录。尝试''新的文件(“../ Res/cat/0.jpg”);'' –

的问题是,该图像基本上在src目录。