Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>When meddling with streams, those kind of behaviors are frequent. Small algorithmic incorrections may cause different symptoms in different environments.</p> <p>You could print "debug" information to the console to better understand where and why is your code stalling. I'm pretty sure its on the read method.</p> <p>There are several things wrong with your code, the most notorious is the block:</p> <pre><code>temp = buf.length - pos; if(temp &lt; 10){ size = temp; } </code></pre> <p>Why? When there are 10 or less bytes left you'll try to read them in the next read(), otherwise you'll potentially try to read too much (the whole file size again). Running in Eclipse, the file is probably being read in the first read(), out of Eclipse apparently not. Try to change your code to something like this:</p> <pre><code>FileInputStream sfis = new FileInputStream(sFile); ByteArrayOutputStream baos = new ByteArrayOutputStream(); byte buf[] = new byte[4096]; int read = sfis.read(buf); while(read&gt;0){ baos.write(buf, 0, read); read = sfis.read(buf); } sfis.close(); </code></pre> <p>Also consider to use <a href="http://commons.apache.org/io/" rel="nofollow">Apache's Java IO utilities</a> code instead of always writing this type of code. IO in Java needs a lot of boilerplate, and a good programmer has better things to do.</p> <p>On a side-note: reading a file to memory is usually a bad idea. Unless you're going to transform its data as a whole in some way, streaming is always better. I don't know what is your program for but please keep this in mind: what happens if you try to read a 10GB file? or several?</p> <p>Happy coding,</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload