处理外部文件的getResourceAsStream [英] Dealing with getResourceAsStream for outside file
问题描述
我遇到一个getResourceAsStream方法的问题 - 它返回null因为错误的目录。问题是我没有一个想法如何定义目录。
I'm having a problem with getResourceAsStream method - it returns null because of wrong directory. The problem is that I do not have an idea how to define the dir.
我的项目结构如下
Project
#src
#com.package
#ExampleClass.java
#dll
#MyFile.dll
当我有
InputStream in = this.getClass().getResourceAsStream("../dll/" + "MyFile.dll");
它返回null。有谁有一个想法如何处理这个问题,如何定义路径?
It returns null. Do anyone have an idea how to deal with this problem and how the path schould be defined?
推荐答案
如果
在 com.package
包中的 ExampleClass
,你需要去两个级别到达CLASSPATH的根目录:
If this
is ExampleClass
in com.package
package, you need to go two levels up to reach the root of the CLASSPATH:
this.getClass().getResourceAsStream("../../dll/" + "MyFile.dll");
假设 / dll
目录直接放在CLASSPATH的根目录。或者直接使用绝对路径:
Assuming /dll
directory is placed directly in the root of your CLASSPATH. Or simply use absolute path:
this.getClass().getResourceAsStream("/dll/" + "MyFile.dll");
如果 /dll/MyFile.dll
不是在你的CLASSPATH(只是打开你的JAR文件并检查它是否存在)你应该使用文件系统机制来打开它。
If the /dll/MyFile.dll
is not on yout CLASSPATH (just open your JAR file and check whether it's there) you should use file system mechanisms to open it.
这篇关于处理外部文件的getResourceAsStream的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!