[英]IndexOutOfBoundException while writing to a file
我正在使用此代碼寫入文件,並且拋出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);
}
我如何檢查還剩多少字節要寫?
當沒有要讀取的內容時, read
方法將返回-1
而不是1
。 因此,循環中的檢查必須為:
for(int i = is.read(array); i != -1; i=is.read(array))
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.