我很困惑getResourceAsStream();
我的包结构如下:
\src
|__ net.floodlightcontroller // invoked getResourceAsStream() here
|__ ...
|__ resources
|__ floodlightdefault.properties //target
|__ ...
Run Code Online (Sandbox Code Playgroud)
我想从floodlightdefault.properties中读取.这是我的代码,位于net.floodlightcontroller包中:
package net.floodlightcontroller.core.module;
// ...
InputStream is = this.getClass().getClassLoader()
.getResourceAsStream("floodlightdefault.properties");
Run Code Online (Sandbox Code Playgroud)
但它失败了,得到了 is == null.所以我想知道究竟是如何getResourceAsStream(file)搜索的file.我的意思是它是通过某些PATH特定的顺序还是以某种顺序起作用的?
如果是这样,如何配置getResourceAsStream()寻找的地方?
谢谢!
Nat*_*nes 13
当您调用时this.getClass().getClassLoader().getResourceAsStream(File),Java会在与指示的类相同的目录中查找该文件this.因此,如果您的文件结构是:
\src
|__ net.floodlightcontroller.core.module
|__ Foo.java
|__ ...
|__ resources
|__ floodlightdefault.properties //target
|__ ...
Run Code Online (Sandbox Code Playgroud)
然后你会打电话给:
InputStream is = Foo.class.getClassLoader()
.getResourceAsStream("..\..\..\resources\floodlightdefault.properties");
Run Code Online (Sandbox Code Playgroud)
更好的是,将包结构更改为:
\src
|__ net.floodlightcontroller.core.module
|__ Foo.java
|__ floodlightdefault.properties //target
|__ ...
Run Code Online (Sandbox Code Playgroud)
并致电:
InputStream is = Foo.class.getClassLoader()
.getResourceAsStream("floodlightdefault.properties");
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
9396 次 |
| 最近记录: |