简体   繁体   中英

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

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

but this returns null

However when I try

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

this returns 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

If you want to to use getResource , then data/dictionary needs to be included within the context of the class path.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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