為什么打印這個(gè)c出現(xiàn)的是整個(gè)文本內(nèi)容,c不是read返回的讀取的總個(gè)數(shù)嗎?
int?c?=?0; while((c=isr.read())!=-1){ ????System.out.print((char)c);//為什么可以打印c?C難不道不是代表讀取的數(shù)量只是一個(gè)計(jì)數(shù)功能???
int?c?=?0; while((c=isr.read())!=-1){ ????System.out.print((char)c);//為什么可以打印c?C難不道不是代表讀取的數(shù)量只是一個(gè)計(jì)數(shù)功能???
2016-10-14
舉報(bào)
2016-10-14
?c表示當(dāng)前讀取到的字節(jié) 。
1、
c = isr.read() ? ?Reads a single character ?讀取單個(gè)character
the next byte of data, or -1 if the end of the file is reached
返回下一個(gè)next byte of data
2、
c = isr.read(byte[] , start,len)?Reads characters into a portion of an array 讀取多個(gè)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,也就數(shù)量
The character read, or -1 if the end of the stream has been reached
? ? ? 如果流讀取結(jié)束,將返回-1
The number of characters read, or -1 if the end of the stream has been reached ?
? ? 如果流讀取結(jié)束,返回-1
所以read()不是表示讀取的計(jì)數(shù),或者位置,表示下一個(gè)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的計(jì)數(shù)。
? ?可以看下out.write(b,off,len);的講解
Writes len bytes from the specified byte array starting at offset off to this file output stream.
? 從off位置開始,從流讀取長(zhǎng)度為lend的byte到b里面
Overrides: write(...) in OutputStream
Parameters:
b the data. 數(shù)據(jù)
off the start offset in the data. 從b的start位置開始
len the number of bytes to write. 讀取長(zhǎng)度為len的byte數(shù)據(jù) ,也就是返回的in.read(b,0,b.length)
}
2017-03-09
// 首先這里按照字符流讀取,即讀到的是Unicode編碼的字符;
2.
“向上兼容”--即:不同數(shù)據(jù)類型的數(shù)據(jù)參與運(yùn)算,數(shù)據(jù)類型要強(qiáng)制轉(zhuǎn)換,轉(zhuǎn)換的方向是
(unsigned)char,(unsigned)short->int->unsigned->long->unsigned long->float->double->longdouble。
3.不管C定義是char還是int ,它都是作為一個(gè)容器來存儲(chǔ)讀到的數(shù)據(jù)
the next byte of data, or -1 if the end of the file is reached;若讀完結(jié)束返回-1,而不是計(jì)數(shù);
4: 在定義C 容器時(shí)候,因?yàn)椤跋蛏霞嫒荨焙妥詈笠祷?1來說;定義為int 最合適;輸出時(shí),則需要強(qiáng)制類型轉(zhuǎn)換;
5: 區(qū)別:
2016-10-23
看清楚兩個(gè)方法是不一樣的好伐!無參數(shù)的read()方法是每次讀取一個(gè)char字符,有參數(shù)的read(buffer, 0, buffer.length)是將讀取的內(nèi)容先放到buffer中去,而返回的是每次讀取的char字符個(gè)數(shù)!
2016-10-14
因?yàn)閖ava的文本(char)本來就是一個(gè)16位無符號(hào)的整數(shù),通過強(qiáng)轉(zhuǎn)(char)c之后,java就會(huì)將返回的整數(shù)轉(zhuǎn)換成char,因?yàn)橥ㄟ^read()的方法獲取的就是char類型數(shù)據(jù),只不過是用整數(shù)形式去表示了,所以可以打印的 是文本內(nèi)容