简体   繁体   English

getResourceAsStream返回null,但File.canRead()返回true

[英]getResourceAsStream returns null but File.canRead() returns true

I have the current file structure 我有当前的文件结构

-Boggle -惊奇

*src
    -java files
*data
    -dictionary.txt
*derived
     -Boggle.jar

Typing 打字

System.out.println(new File(".").getAbsolutePath()

gives

/Boggle/derived/ /惊奇/派生/

I am trying to load the dictionary.txt file with 我正在尝试使用以下方式加载dictionary.txt文件

getClass().getResourceAsStream("../data/dictionary.txt")

but this returns null 但这返回null

However when I try 但是当我尝试

new File("../data/dictionary.txt").canRead()

this returns true. 这返回true。

Help would be much appreciated! 帮助将不胜感激!

data/dictionary is not considered an embedded resource, but is simply an external resource, in this case, a simple File data/dictionary不被视为嵌入式资源,而仅仅是外部资源,在这种情况下,是简单的文件

If you want to to use getResource , then data/dictionary needs to be included within the context of the class path. 如果要使用getResource ,则data/dictionary需要包含在类路径的上下文中。

You can do this by including data/dictionary within one of the applications Jar files or setting the class path to include the parent directory containing the data directory 您可以通过在一个应用程序Jar文件中包含data/dictionary或将类路径设置为包括包含data目录的父目录来完成此操作

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM