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

查看:25
本文介绍了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天全站免登陆