Organize resources in Java

大城市里の小女人 提交于 2019-12-24 12:29:32

问题


How can I organize resources in Java?

For example, when I will use pictures to be embedded in my application, in what folder can I store it? Because when I make my program in an executable .jar file all the resources like pictures, text file, it goes outside the folder where I stored it.


回答1:


A common practice is to reserve a package to resources. You can place it anywhere, depending on the classes needing to access those resources (there's no rule, it's just a matter of organization logic). For example, your class project.gui.Main needs to load some images, you can then create a project.gui.data package and store your resources in it. To load these resources from Main, use the following piece of code :

Main.class.getResource("data/img.png");

This way, you can access the resource with a local path. Never use absolute paths if the application si expected to be packed in a jar.



来源:https://stackoverflow.com/questions/26084134/organize-resources-in-java

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!