2012-03-15 130 views
-1
public BufferedImage readImage(String fileLocation) { 
     BufferedImage img = null; 

     File file = new File("D:\\logo.jpg"); 
      img = ImageIO.read(file); 
} 

抛出错误可以;吨阅读文件imageIO给出错误?

+0

什么是堆栈跟踪?文件是否存在于提供的位置? – Pushkar 2012-03-15 09:02:35

+0

请复制'printStackTrace()'生成的确切错误消息,并将其作为编辑添加到问题中。顺便说一句 - 你的问题是什么(在标题中加入'?'并不意味着问题)?当代码继续从硬编码的String中加载File时,为什么将'fileLocation'指定为参数?为了更快地获得更好的帮助,请发布[SSCCE](http://sscce.org/)(并将图像上传到我们可以看到的地方)。 – 2012-03-15 09:06:37

+0

第一次发布的代码会编译,而最新的编辑代码不会。编辑时请多加小心。 – 2012-03-15 09:08:23

回答

0

的代码应该像如下,

BufferedImage img = null; 
try { 
    img = ImageIO.read(new File("D:\\logo.jpg")); 
} catch (IOException e) { 
     System.out.println("Exception is --- "+ e.getMessage()); 
     e.printStackTrace(); 
} 

然后你就可以追查问题的真正原因,因为我们打印的消息和堆栈跟踪,如果发生异常。