Eclipse getResourceAsStream返回null [英] Eclipse getResourceAsStream returning null

查看:276
本文介绍了Eclipse getResourceAsStream返回null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法获取getResourceAsStream来查找文件。我把文件放在顶级目录,目标目录等中,并且在前面也尝试过一个/。每次返回null。

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.

任何建议?谢谢。

public class T {
 public static final void main(String[] args) {

  InputStream propertiesIS = T.class.getClassLoader().getResourceAsStream("test.txt");

  System.out.println("Break");
 }
}


推荐答案

您的文件test.txt到同一个目录,你的类的java文件是(相同的包)。然后使用

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

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

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

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 getResourceAsStream返回null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆