最近中文字幕高清中文字幕无,亚洲欧美高清一区二区三区,一本色道无码道dvd在线观看 ,一个人看的www免费高清中文字幕

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定

4.3中的Copy方法下為什么要有while?

while(((b=in.read(buf,0,buf.length))!=-1))

{

out.write(buf,0,b);

}

++++++++++++++

能不能像下面這樣》

b=in.read(buf,0,buf.length);

out.write(buf,0,b);

out.flush();//最好加上




正在回答

3 回答

這個(gè)while循環(huán)就是為了讀取完要讀取的東西。同時(shí)也像你說(shuō)的為了防止它為空。而且,按照正常情況,buf是字節(jié)數(shù)組,應(yīng)該是1024字節(jié)的倍數(shù)吧,有時(shí)候一次讀不完,所以用個(gè)while循環(huán)來(lái)判定。

望采納,謝謝。

1 回復(fù) 有任何疑惑可以回復(fù)我~
#1

謎之米

那如果一次文件沒(méi)讀完。第二次不就會(huì)讀到重復(fù)的嗎?或者in.read會(huì)自動(dòng)將未讀的放入buf?
2017-01-16 回復(fù) 有任何疑惑可以回復(fù)我~
#2

KeT 回復(fù) 謎之米

如你所想,底層確實(shí)寫好了。你想想如果沒(méi)寫好的話,如果照著你之前的思路,不就是會(huì)一直重復(fù)讀取,死循環(huán)了嘛。
2017-01-19 回復(fù) 有任何疑惑可以回復(fù)我~
#3

謎之米 回復(fù) KeT

對(duì)啊。是實(shí)現(xiàn)了 但是我不知道為什么啊 第一次循環(huán)過(guò)后不會(huì)覆蓋 原理是什么 是read有判斷機(jī)制還是什么 ?老師也沒(méi)講清楚啊,所以我要問(wèn)啊。。
2017-01-20 回復(fù) 有任何疑惑可以回復(fù)我~
#4

壯丹田 提問(wèn)者

非常感謝!
2017-05-21 回復(fù) 有任何疑惑可以回復(fù)我~
查看1條回復(fù)

這是底層實(shí)現(xiàn)代碼,我把主要的注釋寫出來(lái)了。自己看著來(lái),不清楚就再探究下。

?public synchronized int read(byte b[], int off, int len)

? ? ? ? throws IOException

? ? {

? ? ? ? getBufIfOpen(); // Check for closed stream

? ? ? ? if ((off | len | (off + len) | (b.length - (off + len))) < 0) {

? ? ? ? ? ? throw new IndexOutOfBoundsException();

? ? ? ? } else if (len == 0) {

? ? ? ? ? ? return 0;

? ? ? ? }


? ? ? ? int n = 0;

? ? ? ? for (;;) {

? ? ? ? ? ? int nread = read1(b, off + n, len - n); ? ?//關(guān)鍵,調(diào)用read1,達(dá)到不重復(fù)讀的目的

? ? ? ? ? ? if (nread <= 0)

? ? ? ? ? ? ? ? return (n == 0) ? nread : n;

? ? ? ? ? ? n += nread;

? ? ? ? ? ? if (n >= len)

? ? ? ? ? ? ? ? return n;

? ? ? ? ? ? // if not closed but no bytes available, return

? ? ? ? ? ? InputStream input = in;

? ? ? ? ? ? if (input != null && input.available() <= 0)

? ? ? ? ? ? ? ? return n;

? ? ? ? }

? ? }

private int read1(byte[] b, int off, int len) throws IOException {

? ? ? ? int avail = count - pos;

? ? ? ? if (avail <= 0) {

? ? ? ? ? ? /* If the requested length is at least as large as the buffer, and

? ? ? ? ? ? ? ?if there is no mark/reset activity, do not bother to copy the

? ? ? ? ? ? ? ?bytes into the local buffer. ?In this way buffered streams will

? ? ? ? ? ? ? ?cascade harmlessly. */

? ? ? ? ? ? if (len >= getBufIfOpen().length && markpos < 0) {

? ? ? ? ? ? ? ? return getInIfOpen().read(b, off, len);

? ? ? ? ? ? }

? ? ? ? ? ? fill();

? ? ? ? ? ? avail = count - pos;

? ? ? ? ? ? if (avail <= 0) return -1;

? ? ? ? }

? ? ? ? int cnt = (avail < len) ? avail : len;

? ? ? ? System.arraycopy(getBufIfOpen(), pos, b, off, cnt); ? ? ? ?//讀取范圍,pos變量是關(guān)鍵

? ? ? ? pos += cnt; ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?//關(guān)鍵所在,pos變量是成員變量,會(huì)被記錄,這也是為什么不會(huì)重復(fù)讀

? ? ? ? return cnt;

? ? }


0 回復(fù) 有任何疑惑可以回復(fù)我~

我的意思是既然out.write(buf,0,b);已經(jīng)把數(shù)據(jù)都寫進(jìn)去了,為什么之前還要有while?

0 回復(fù) 有任何疑惑可以回復(fù)我~
#1

壯丹田 提問(wèn)者

是為了防止srcFile是空的嗎?不是空的,才輸入?
2017-01-09 回復(fù) 有任何疑惑可以回復(fù)我~
#2

灬諾諾

while是為了避免讀取文件為空或文件太大一次讀取不完
2017-02-26 回復(fù) 有任何疑惑可以回復(fù)我~

舉報(bào)

0/150
提交
取消

4.3中的Copy方法下為什么要有while?

我要回答 關(guān)注問(wèn)題
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)