亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定

為什么打印這個c出現的是整個文本內容,c不是read返回的讀取的總個數嗎?

int?c?=?0;
while((c=isr.read())!=-1){
????System.out.print((char)c);//為什么可以打印c?C難不道不是代表讀取的數量只是一個計數功能???


正在回答

4 回答

?c表示當前讀取到的字節 。

1、

c = isr.read() ? ?Reads a single character ?讀取單個character

the next byte of data, or -1 if the end of the file is reached

返回下一個next byte of data

2、

c = isr.read(byte[] , start,len)?Reads characters into a portion of an array 讀取多個charachters?

返回the total number of bytes read into the buffer, or -1 if there is no more data because the end of the file has been reached.

? 返回total所有的number,也就數量


The character read, or -1 if the end of the stream has been reached

? ? ? 如果流讀取結束,將返回-1

The number of characters read, or -1 if the end of the stream has been reached ?

? ? 如果流讀取結束,返回-1

所以read()不是表示讀取的計數,或者位置,表示下一個byte.


/****************************************常用用法


byte[] b = new byte[8 * 1024];// 8kb

// System.out.println(b.length);

// new byte

int a = 0;

FileOutputStream out = new FileOutputStream(destfile);

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

out.write(b, 0, a);

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

這里的a表示總共。total的計數。

? ?可以看下out.write(b,off,len);的講解

Writes len bytes from the specified byte array starting at offset off to this file output stream.

? 從off位置開始,從流讀取長度為lend的byte到b里面

Overrides: write(...) in OutputStream

Parameters:

b the data. 數據

off the start offset in the data. 從b的start位置開始

len the number of bytes to write. 讀取長度為len的byte數據 ,也就是返回的in.read(b,0,b.length)

}


0 回復 有任何疑惑可以回復我~
#1

糖醋肉3984350 提問者

我這么理解,也就是說單個字符的時候表示讀取的下一個字符,用數組緩沖讀取多個時,代表總數嘛?
2016-10-14 回復 有任何疑惑可以回復我~
#2

張博

兩個不一樣,你可以看看那個英語翻譯。
2016-10-15 回復 有任何疑惑可以回復我~
int?c?=?0;
while((c=isr.read())!=-1){?.?
????System.out.print((char)c);

// 首先這里按照字符流讀取,即讀到的是Unicode編碼的字符;

2.

“向上兼容”--即:不同數據類型的數據參與運算,數據類型要強制轉換,轉換的方向是

(unsigned)char,(unsigned)short->int->unsigned->long->unsigned long->float->double->longdouble。

3.不管C定義是char還是int ,它都是作為一個容器來存儲讀到的數據

the next byte of data, or -1 if the end of the file is reached;若讀完結束返回-1,而不是計數;

4: 在定義C 容器時候,因為“向上兼容”和最后要返回-1來說;定義為int 最合適;輸出時,則需要強制類型轉換;

5: 區別:

int?c?=?read();???//?存入C容器中,結束返回-1;
int?c?=?read(byte,star,length);???//計數,結束返回-1;


1 回復 有任何疑惑可以回復我~

看清楚兩個方法是不一樣的好伐!無參數的read()方法是每次讀取一個char字符,有參數的read(buffer, 0, buffer.length)是將讀取的內容先放到buffer中去,而返回的是每次讀取的char字符個數!

1 回復 有任何疑惑可以回復我~
#1

糖醋肉3984350 提問者

嗯嗯!!謝謝旁友!你這么一說我就理解了
2016-10-23 回復 有任何疑惑可以回復我~

因為java的文本(char)本來就是一個16位無符號的整數,通過強轉(char)c之后,java就會將返回的整數轉換成char,因為通過read()的方法獲取的就是char類型數據,只不過是用整數形式去表示了,所以可以打印的 是文本內容

1 回復 有任何疑惑可以回復我~
#1

糖醋肉3984350 提問者

我記得老師說,c代表總個數,所以我就有點蒙了。既然是個數又怎么強轉成文本呢。。。
2016-10-14 回復 有任何疑惑可以回復我~

舉報

0/150
提交
取消

為什么打印這個c出現的是整個文本內容,c不是read返回的讀取的總個數嗎?

我要回答 關注問題
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號