Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

getResourceAsStream() returns a stream whose content is empty

I have a small Java project in eclipse. The project has an src and bin folder (typical) and src is added to class path. I am running on Windows.

The following call works:

System.out.println(TestINI.class.getResource("happytest.ini"));

It prints the path of file:

file:/D:/work/baton/Touch/JTouch/bin/com/interra/tests/happytest.ini

I checked that the contents of ini file mentioned above have expected content.

But reading from the file as follows:

InputStream is = TestINI.class.getResourceAsStream("happytest.ini");

gives me an input stream which has no content. i.e. it returns a file with zero bytes.

My Eclipse is 3.6 Helios 64 bit running on Windows 7.

Google searching revealed cases where the resource path or input stream was null but that is not the case with me.

The class path entries are as follows:

<classpathentry kind="src" path="src"/>
... other class path entries for libraries like ini4j
<classpathentry kind="output" path="bin"/>
like image 583
Shailesh Kumar Avatar asked Nov 15 '22 01:11

Shailesh Kumar


1 Answers

I tried out your example and it worked for me with getResource() and getResourceAsStream() as well. I know that it is not reassuring.

Anyway, it is interesting that your stream has no content, try this out:

new FileInputStream(new File(TestINI.class.getResource("happytest.ini").toURI()));

If you read this stream and there is no content, then something is wrong with your file. For testing I called the read() method on the Streams.

like image 113
Zsolt Avatar answered Dec 16 '22 20:12

Zsolt