使用getResource()获取资源 [英] Get a resource using getResource()
本文介绍了使用getResource()获取资源的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要在java项目中获取资源图像文件。我正在做的是:
I need to get a resource image file in a java project. What I'm doing is:
URL url = TestGameTable.class.getClass().
getClassLoader().getResource("unibo.lsb.res/dice.jpg");
目录结构如下:
unibo/
lsb/
res/
dice.jpg
test/
..../ /* other packages */
事实是我总是得到文件不存在。我尝试了很多不同的路径,但我无法解决这个问题。
任何提示?
The fact is that I always get as the file doesn't exist. I have tried many different paths, but I couldn't solve the issue. Any hint?
推荐答案
TestGameTable.class.getResource("/unibo/lsb/res/dice.jpg");
- 引用斜杠来表示类路径的根
- 斜杠而不是路径中的点
- 你可以直接在课堂上调用
getResource()
。 - leading slash to denote the root of the classpath
- slashes instead of dots in the path
- you can call
getResource()
directly on the class.
这篇关于使用getResource()获取资源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文