Eclipse getResourceAsStream 返回 null
我无法让 getResourceAsStream 查找文件。我已将文件放在顶级目录、目标目录等中,并在前面尝试了“/”。每次都返回null。
有什么建议吗?谢谢。
public class T {
public static final void main(String[] args) {
InputStream propertiesIS = T.class.getClassLoader().getResourceAsStream("test.txt");
System.out.println("Break");
}
}
I cannot get getResourceAsStream to find a file. I have put the file in the top level dir, target dir, etc, etc and have tried it with a "/" in front as well. Everytime it returns null.
Any suggestions ? Thanks.
public class T {
public static final void main(String[] args) {
InputStream propertiesIS = T.class.getClassLoader().getResourceAsStream("test.txt");
System.out.println("Break");
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
将文件“test.txt”放入类的java文件所在的同一目录中(同一包)。然后使用
This就可以了,因为eclipse会自动将文件作为资源复制到类路径中。使用命令行时,您必须手动执行此操作。
Put your file "test.txt" into the same directory where the java file of your class is (same package). Then use
This works, because eclipse automatically copies the file as a resource to the classpath. When using the command line, you have to do this by hand.
您不需要将这些文件添加到同一目录即可使其正常工作。
当我创建一个新的包和源文件夹来保存我的 junit 测试时,我遇到了这种症状。测试将失败,因为 getResourceAsStream 返回 null。
修复方法如下:
在 Eclipse Project Explorer 视图中右键单击该类(在我的例子中是新的 junit 测试类)
Build Path ->配置构建路径-> Java 构建路径 ->来源选项卡 ->添加文件夹
选择保存您的文件的文件夹。
You shouldn't need to add these files to the same directory to get it to work.
I was getting this symptom when I created a new Package and Source Folder to hold my junit tests. The tests would fail because getResourceAsStream was returning null.
Here's how to fix it:
Right-click the class (in my case, the new junit test class) in the Eclipse Project Explorer View
Build Path -> Configure Build Path -> Java Build Path -> Source Tab -> Add Folder
Select the folder that holds your files.
有时您需要明确告诉 Eclipse 将哪些类型的文件从源文件夹复制到分发(类)文件夹。
我有 Eclipse SDK,版本:3.7.1,构建 ID:M20110909-1335,Indigo 和在此
我做了以下更改。
项目->属性-> Java 构建路径 ->来源(选项卡)->包含(列表项)->编辑(按钮)以将 */.txt 添加到现有的 */.java。
Sometimes you need to tell eclipse explicitly what types of files to copy from the source folder to the distribution (classes) folder.
i have Eclipse SDK, Version: 3.7.1, Build id: M20110909-1335, Indigo and in this
i did the following changes.
Project -> Properties -> Java Build Path -> Source (tab) -> Included (list item) -> Edit (button) to add */.txt to the existing */.java.
另请确保您的文件与首选项>Java>编译器>构建>输出文件夹>过滤的资源:的任何模式都不匹配。
例如,如果您在该字段中设置 *.txt,则不会将 test.txt 获取到构建输出。
Also make sure your file does not match any pattern of Preference>Java>Compiler>Building>OutputFolder>Filtered resources:.
For example, if you set *.txt in that field, you will not get test.txt to the build output.
将我的 /resources/ 文件夹放入我的 /bin/ 文件夹中为我解决了这个问题。
Putting my /resources/ folder into my /bin/ folder solved this issue for me.
老问题,但我遇到了同样的问题,并且没有一个答案对我有用(或者我不明白它们)。
在 Eclipse 中,刷新项目目录以使 Eclipse 知道新文件已添加到资源中。为此,右键单击包资源管理器视图中的项目(最顶层)目录,然后单击“刷新”。如果您从 eclipse 外部编辑现有资源文件,则效果相同:刷新以使 eclipse 知道所做的编辑。
Old question but I just had the same problem and none of the answers worked for me (or I didn't understand them).
In Eclipse, refresh the project directory to make Eclipse aware that a new file has been added to resources. For this, right-click on the project (topmost) directory in the package explorer view, then hit "Refresh". The same if you edit an existing resource file from outside eclipse: refresh to make eclipse aware of the edits.