开发者

Netbeans: Accessing resources in other packages

I have switched ove开发者_如何学Cr to Netbeans and am finding accessing files in packages quite perplexing. Usually I would just have everything in the one folder.

I have created packages in my project which is structured like so:

project_name  
......Source Packages  
............Game  
............Players  
............Resources  
..................Levels  
..................Images  

I want to access a text file that resides in the Resources.Levels package from my Gui class which resides in my Game package. How would I do this? Ideally I want to create a String = "mapFile.txt"

Thanks


MyClass.class.getResources("/Resources/Image/myImage.jpg");


This has nothing to do with NetBeans, but everything to do with Java. To access a resource in the classpath, you should use Class.getResourceAsStream() method. Read its documentatino carefully. In your case, you should use

Gui.class.getResourceAsStream("/Resources/Levels/mapFile.txt");

Also, packages in Java should be in all lowercase.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜