从jar读取txt文件失败但读取图像有效 [英] Reading txt file from jar fails but reading image works

查看:113
本文介绍了从jar读取txt文件失败但读取图像有效的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个困扰我好几天的问题......我检查了类似的问题,但找不到解决方案。

I have an issue which has been bothering me for days... I checked similar questions but couldn't find a solution.

我使用的是NetBeans IDE。我构建了项目jar文件,即Clock.jar,其中包含一个名为clock的文件夹,其中包含一些图像,一个文本文件和所有项目类。
以下用于创建图像图标的代码可以使用

I use NetBeans IDE. I build the project jar file i.e "Clock.jar" which contains a "clock" named folder in which some images, a text file and all project classes are found. The following code for creating an image icon works

return new ImageIcon(getClass().getResource("/clock/button_close.png"));

但以下用于阅读文本文件的代码失败

But the following code for reading the text file fails

InputStream name = getClass().getResourceAsStream("/clock/input.txt");
BufferedReader reader = new BufferedReader(new InputStreamReader(name));

您可能已经猜到NullPointer Exception被抛出意味着它可能无法找到该文件。

As you may have guessed the NullPointer Exception is thrown meaning probably it couln't locate the file.

但是如何成功构建图像图标(通过传递从getResource返回的URL)但是找不到txt文件(通过从getResourceAsStream传递输入流) )。

But how come the image icon is constructed successfully (by passing it the URL returned from getResource) but the txt file cannot be found (by passing it an input stream from getResourceAsStream).

提前致谢,对于任何答案(我的意思是:) :)

Thanks in advance, for any answer ( I mean it :) )

jar -tvf Clock.jar
0 Wed May 15 14:44:36 EEST 2013 META-INF/
202 Wed May 15 14:44:34 EEST 2013 META-INF/MANIFEST.MF
0 Wed May 15 14:44:36 EEST 2013 clock/
649 Wed May 15 14:44:36 EEST 2013 clock/Clock$1$1.class
789 Wed May 15 14:44:36 EEST 2013 clock/Clock$1.class
2026 Wed May 15 14:44:36 EEST 2013 clock/Clock.class
709 Wed May 15 14:44:36 EEST 2013 clock/ClockDialog$1.class
830 Wed May 15 14:44:36 EEST 2013 clock/ClockDialog$2.class
750 Wed May 15 14:44:36 EEST 2013 clock/ClockDialog$3.class
713 Wed May 15 14:44:36 EEST 2013 clock/ClockDialog$4.class
741 Wed May 15 14:44:36 EEST 2013 clock/ClockDialog$5.class
708 Wed May 15 14:44:36 EEST 2013 clock/ClockDialog$6.class
1081 Wed May 15 14:44:36 EEST 2013 clock/ClockDialog$7.class
981 Wed May 15 14:44:36 EEST 2013 clock/ClockDialog$8.class
9640 Wed May 15 14:44:36 EEST 2013 clock/ClockDialog.class
702 Wed May 15 14:44:36 EEST 2013 clock/ClockFrame$1.class
708 Wed May 15 14:44:36 EEST 2013 clock/ClockFrame$2.class
734 Wed May 15 14:44:36 EEST 2013 clock/ClockFrame$3.class
743 Wed May 15 14:44:36 EEST 2013 clock/ClockFrame$4.class
531 Wed May 15 14:44:36 EEST 2013 clock/ClockFrame$5.class
1046 Wed May 15 14:44:36 EEST 2013 clock/ClockFrame$6.class
9464 Wed May 15 14:44:36 EEST 2013 clock/ClockFrame.class
782 Wed May 15 14:44:36 EEST 2013 clock/ErrorReporter.class
1826 Wed May 15 14:44:36 EEST 2013 clock/IconButton.class
2693 Wed May 15 14:44:36 EEST 2013 clock/MessagePool.class
2824 Wed May 15 14:44:36 EEST 2013 clock/SystemInfo.class
2212 Wed May 15 14:44:36 EEST 2013 clock/button_close.png
6540 Wed May 15 14:44:36 EEST 2013 clock/button_close_highlighted.png
5668 Wed May 15 14:44:36 EEST 2013 clock/input.txt


推荐答案

Looki再次密切关注源代码,尝试..

Looking at the source again closely, try..

URL url = getClass().getResource("/clock/input.txt"); 
InputStream name = url.openStream(); 

我记得 getResourceAsStream 变种处理路径与简单的 getResource 略有不同 - 我相信它应该适用于后者。

I recall that the getResourceAsStream variant deals with paths slightly differently than simply getResource - I am sure it should work for the latter one.

这篇关于从jar读取txt文件失败但读取图像有效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆