为什么打印这个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
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)
}
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;
举报