GetResourceAsStream () returns a stream whose contents are empty

I have a small Java project in eclipse. The project has a src and bin folder (typical), and src is added to the class path. I work in windows.

The following call is made:

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

It prints the file path:

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

I checked that the contents of the ini file mentioned above were expecting the contents.

But reading from a file looks like this:

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

gives me an input stream that has no content. that is, it returns a file with zero bytes.

My Eclipse is a 64-bit 64-bit Helios server running on Windows 7.

A Google search revealed cases where the resource path or input stream was null, but this is not the case.

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"/> 
+4
source share
3 answers

That is what Jim Harrison said - you edited the file in 'src' and read from 'bin' ...

0
source

I tried your example and it worked for me with getResource() and getResourceAsStream() . I know this is not encouraging.

In any case, it is interesting that your stream has no content, try this:

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

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

+1
source

I think I understood the problem. For ini files, my eclipse opened notebook by default. The changes made by me in the notepad ini file, they did not affect the eclipse side properly. Today, when I edited the file through the Eclipse Text Editor, they were correctly reflected in the getResourceAsStream () calls. In fact, after editing via notepad, the resource was not synchronized in eclipse.

0
source

Source: https://habr.com/ru/post/1344163/


All Articles