问题
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