我目前在Java中设置了一个项目,在Eclipse中使用以下目录结构:

在我的代码中,我有以下几行:
InputStream is = this.getClass().getClassLoader().getResourceAsStream("resources/config");
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(is));
Run Code Online (Sandbox Code Playgroud)
但是,InputStream is总是被赋值为null,这会在到达第二行时导致崩溃.我知道它与我如何设置它正在寻找的路径有关,但我无法弄清楚它为什么不起作用.
您的config文件位于项目中,位于文件系统的某个位置.
但是,Eclipse并没有将它放在类路径上.要强制它在类路径上,请右键单击您的文件夹并将其添加为源文件夹.Eclipse然后将它添加到类路径的根目录.你可以用它来检索它
InputStream is = this.getClass().getResourceAsStream("/config");
Run Code Online (Sandbox Code Playgroud)
Eclipse将所有内容放在resources源路径文件夹的根目录下.因此
resources/config
将出现在类路径中
/config
/qbooksprintfix/FileChecker
/qbooksprintfxi/FilePurgeHandler
/...
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
14789 次 |
| 最近记录: |