Java - where to store extra resources files
My co开发者_如何学JAVAde require one PNG file. Where should I put it ? I saw some people put them along with .java, then use getClassLoader().getResourceAsStream.
Personally, I feel it is not clean to mix resource files with source code. Is this a OK approach ?
That is a very common and accepted practice. It has the advantage that the PNG file will be bundled inside of the JAR file along with the code. So you do not have to worry about file system issues and installation processes. If your code is there, so will the resource.
You can have multiple source folders for the same project, one for Java, one for resources. If you are using Maven, it is recommended to have that kind of structure. However, mixing the PNG (and properties, and XML) files into the source tree is not a problem, either (especially since the separation would complicate the build process if you do not use Maven).
The only things you should keep apart from the source (and the compiled result) is user-editable data, such as configuration files. This may apply here if you want the user to be able to replace the image. If it is fixed (or a default is provided), bundle it up with your program to reduce the number of movable (= breakable) parts.
精彩评论