Reputation: 83
I'm using this code to write to a file , and it throws IndexOutOfBoundException
.
InputStream is = res.openStream();
FileOutputStream fos = new FileOutputStream(file);
byte[] array = new byte[1024];
for(int i = is.read(array); i != 1; i=is.read(array)) {
fos.write(array, 0, i);
}
how can I check how many bytes left to write?
Upvotes: 0
Views: 89
Reputation: 3191
When there is nothing to read, the read
method returns -1
and not 1
.
Therefore the check in your loop must be:
for(int i = is.read(array); i != -1; i=is.read(array))
Upvotes: 3