如何将包标记为资源文件夹? [英] How to mark package as a resource folder?
问题描述
我有一个Intellij 12的目录结构:
I have a dir structure for Intellij 12:
...
...test
- java
- com.mycompany.myproject
- package1 (contains code, etc,.)
- resourcePackage (want to contain .json files but can't mark as a resource)
- myOtherJunk.json
- o o o
- resources
- aResource.json
如果我右键单击我的包名(com.mycompany.myproject),我只能添加包而不是目录(如现有资源文件夹的目录)。
The thing is if I right click on my package name (com.mycompany.myproject) I can only add packages and not directories (like that of an existing resource folder).
但是,我不想将我现在要读取的.json文件的现有资源文件夹用于我的测试类。
However, I don't want to use that existing resource folder for the .json files that I'm going to read into per my test class.
所以,我需要一些东西来支持:
So, I need something to support:
// this already works for the resources directory per the .json file but doesn't for the
// myOtherJunk.json per the resourcePackage.
InputStream is = MyClassTest.class.getResourceAsStream("aResource.json");
推荐答案
这可以通过多种方式解决。一个好的方法的例子是以下文件夹结构:
This can be solved in several ways. An example of a good approach would be the following folder structure:
src
main
java
resources
test
java
resources
当这是完成后,将所有java类放在 src / main / java / com.mycompany
包和 / src / main / resources / com下的所有资源下/ mycompany
文件夹。
When this is done, you put all you java classes under src/main/java/com.mycompany
package and any resources under /src/main/resources/com/mycompany
folder.
要将它们链接在一起,请转到项目属性,然后找到路径选项卡。将 src / main / java
和 src / main / resources
标记为源文件夹。 (参见随附的屏幕截图)
To link them together, go to the project properties, and find the Path tab. Mark the src/main/java
and src/main/resources
as source folders. (see the screen-shot attached)
如果将它们链接在一起,您将能够使用getResourceAsStream()方法。
如果您想知道为什么要使用以下文件夹结构 - 这是标准的 maven 保持干净整洁的方式。
If you link them together, you'll be able to use getResourceAsStream() method. If you wonder why you should use the following folder structure - this is standard maven way of keeping things clean and tidy.
这篇关于如何将包标记为资源文件夹?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!