Take the 2-minute tour ×

I'm a bit stuck on a project I'm working on where I want to load in a text file from another folder. I'm using Netbeans, and have, for the purposes of this problem, two folders, one with my class in, and one with the resource in.

The class is in ../misc/[ClassName] and the text file I want to load is in ../resources/[Name].txt

This sounds unbelievably simple, and having done Java for quite a while I really should know this, I assumed the best way of loading this file rather than using just a FileReader, would be to use getResourceAsStream, as shown:

InputStream is = 
        this.getClass().getClassLoader().getResourceAsStream(_filename + ".txt");

Ideally saving me time and not having to hardcode in a filepath. Now, my problem is I constantly keep getting null returned from the above code, and indeed any other permutation I can put on it. I've tried /resource/filename.txt or resource/filename.txt as parameters, using .getClass().getResourceAsStream as opposed to getClassLoader(), everything is still returning the same result.

Just to avoid any confusion, I've checked the filename and its definitely right and in the right case etc. as well, so that isn't the issue. Any ideas? I know I could just use FileReader to accomplish the same goal, but its kind of a principle thing now!

share|improve this question
add comment

4 Answers

The class is apparently loaded by a different classloader which doesn't know about the resource at all. Rather use the context's ClassLoader obtained from the current Thread instead. It knows everything.

InputStream is = Thread.currentThread().getContextClassLoader().getResourceAsStream(_filename + ".txt");
share|improve this answer
add comment

I use IntelliJ and have found a common "gotcha" for me is forgetting to define the appropriate resource pattern to include .txt files, .css files, etc. This means that when the build process runs it will exclude these files from the jar file it creates, causing calls to getResource() to return null.

I suspect a similar thing might be going on with your Netbeans environment so might be worth checking.

share|improve this answer
gosh, why don't they add .txt files by default... this saved me an hour –  somid3 Jul 21 '12 at 22:33
add comment

Where does your classpath start? Does it start from misc or ..?

Can you post the value of this.getClass().getName()?

If it returns misc.ClassName you must use this.getClass().getResourceAsStream("/resources/filename.txt").

share|improve this answer
Yep, dropping out the getClassLoader() should fix it. At least, it did for me doing: getClass().getResourceAsStream("../resources/filename.txt") –  Ash Feb 18 '10 at 12:33
@Ash: this makes no difference. The cls.getResourceAsStream() is just a shorthand for cls.getClassLoader().getResourceAsStream(). –  BalusC Feb 18 '10 at 12:43
@BalusC: it made all the difference in a test app I wrote trying to replicate the problem. With the extra "getClassLoader()" it returned null, without it returned the stream (or URL for getResource()). –  Ash Feb 18 '10 at 12:48
I had tried a zillion combination before coming across this post. The key here as @Chandru pointed out is to check the value of this.getClass().getName() & then pass the file path accordingly. –  mindReader Feb 22 '13 at 10:05
add comment

I got the same problem as David Meadows,
it has worked since 2008 and no problem. Read all the post regarding getResourceAsStream( ) here. My problem is that I can read some of the files from the jar file. All png file can be read, but the wav files not. The strange thing is if I rename the .wav file to .png. The files is possible to load and play!

When I deploy the application it works, could it be IntelliJ somehow ?

Note: I know the .wav files is there beacuse its possible to reach with JarFile and JarEntry.

Solution in intelliJ: In project settings, add the folder with the resource files, in my case sound files, as source. Add

?*.wav (or your pref filetype)

in Settings->Compiler Resource patterns.

share|improve this answer
add comment

Your Answer


