Eclipse getResourceAsStream 返回 null

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2195445/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 19:09:49  来源:igfitidea点击:

Eclipse getResourceAsStream returning null

eclipsenullresources

提问by Chris

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.

我无法使用 getResourceAsStream 来查找文件。我已将该文件放在顶级目录、目标目录等中,并尝试在前面加上“/”。每次都返回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");
 }
}

回答by tangens

Put your file "test.txt" into the same directory where the java file of your class is (same package). Then use

把你的文件“test.txt”放到你的类的java文件所在的同一个目录中(同一个包)。然后使用

T.class.getResourceAsStream( "test.txt" );

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.

这是有效的,因为 eclipse 会自动将文件作为资源复制到类路径。使用命令行时,您必须手动执行此操作。

回答by Ben L.

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.

当我创建一个新的包和源文件夹来保存我的 junit 测试时,我遇到了这个症状。测试将失败,因为 getResourceAsStream 返回 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

右键单击 Eclipse Project Explorer 视图中的类(在我的例子中是新的 junit 测试类)

Build Path -> Configure Build Path -> Java Build Path -> Source Tab -> Add Folder

构建路径 -> 配置构建路径 -> Java 构建路径 -> 源选项卡 -> 添加文件夹

Select the folder that holds your files.

选择保存文件的文件夹。

回答by ksridhar

Sometimes you need to tell eclipse explicitly what types of files to copy from the source folder to the distribution (classes) folder.

有时您需要明确地告诉 eclipse 将哪些类型的文件从源文件夹复制到分发(类)文件夹。

i have Eclipse SDK, Version: 3.7.1, Build id: M20110909-1335, Indigo and in this i did the following changes.

我有 Eclipse SDK,版本:3.7.1,构建 ID:M20110909-1335,Indigo,在此我做了以下更改。

Project -> Properties -> Java Build Path -> Source (tab) -> Included (list item) -> Edit (button) to add */.txt to the existing */.java.

Project -> Properties -> Java Build Path -> Source (tab) -> Included (list item) -> Edit (button) 将 * /.txt添加到现有的 * /.java。

回答by CamHart

Putting my /resources/ folder into my /bin/ folder solved this issue for me.

将我的 /resources/ 文件夹放入我的 /bin/ 文件夹为我解决了这个问题。

回答by dariober

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, refreshthe 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.

在 Eclipse 中,刷新项目目录以使 Eclipse 知道一个新文件已添加到资源中。为此,右键单击包资源管理器视图中的项目(最顶层)目录,然后点击“刷新”。如果您从 eclipse 外部编辑现有资源文件,则相同:刷新以使 eclipse 知道这些编辑。

回答by chengdong

Also make sure your file does not match any pattern of Preference>Java>Compiler>Building>OutputFolder>Filtered resources:.

还要确保您的文件不匹配Preference>Java>Compiler>Building>OutputFolder>Filtered resources: 的任何模式。

For example, if you set *.txtin that field, you will not get test.txt to the build output.

例如,如果在该字段中设置*.txt,则不会将 test.txt 发送到构建输出。