本文介绍了使用 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()
.
这篇关于使用 getResource() 获取资源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!