开发者

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 ab开发者_开发技巧ove 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"/>


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.


That's exactly what Jim Garrison said - you edited the file in 'src' and read from 'bin'...


I think I figured out the problem. For the ini files, my eclipse was opening notepad by default. The changes I made through notepad in the ini file, they didn't reflect on eclipse side properly. Today when I edited the file through Eclipse's Text Editor, they reflected properly in the getResourceAsStream() calls. Essentially after editing through notepad, the resource was out of sync in eclipse.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜