為什么打印這個c出現的是整個文本內容,c不是read返回的讀取的總個數嗎?
int?c?=?0; while((c=isr.read())!=-1){ ????System.out.print((char)c);//為什么可以打印c?C難不道不是代表讀取的數量只是一個計數功能???
int?c?=?0; while((c=isr.read())!=-1){ ????System.out.print((char)c);//為什么可以打印c?C難不道不是代表讀取的數量只是一個計數功能???
2016-10-14
舉報
2016-10-14
?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)
}
2017-03-09
// 首先這里按照字符流讀取,即讀到的是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: 區別:
2016-10-23
看清楚兩個方法是不一樣的好伐!無參數的read()方法是每次讀取一個char字符,有參數的read(buffer, 0, buffer.length)是將讀取的內容先放到buffer中去,而返回的是每次讀取的char字符個數!
2016-10-14
因為java的文本(char)本來就是一個16位無符號的整數,通過強轉(char)c之后,java就會將返回的整數轉換成char,因為通過read()的方法獲取的就是char類型數據,只不過是用整數形式去表示了,所以可以打印的 是文本內容