0
byte data[] = new byte[1024];
fout = new FileOutputStream(fileLocation);
ByteBuffer bb = ByteBuffer.allocate(i+i); // i is size of download
ReadableByteChannel rbc = Channels.newChannel(url.openStream());
while( (dat = rbc.read(bb)) != -1)
{
bb.get(data);
fout.write(data, 0, 1024); // write the data to the file
speed.setText(String.valueOf(dat));
}
在這段代碼我嘗試從給定的URL下載一個文件,但該文件沒有完成所有它的方式。
我不知道發生了什麼錯誤,是ReadableByteChannel的錯嗎?或者我沒有正確地將ByteBuffer的字節放入Byte []中。
使用'fout.flush()嘗試; fout.close();'fout.write後'()' – Prabhaker
@Prabhaker應該.close( )在那裏?因爲這可能意味着我將無法在循環中使用該通道。 – user2519193
對不起,我不是在循環中的意思。你可以在循環後使用close()(當你的使用完成時你必須關閉它們); – Prabhaker