前言:这两个方法在抽象类InputStream中都是作为抽象方法存在的
1.read()
read() : 从输入流中读取数据的下一个字节,返回0到255范围内的int字节值。如果因为已经到达流末尾而没有可用的字节,则返回-1。在输入数据可用、检测到流末尾或者抛出异常前,此方法一直阻塞。
由帮助文档中的解释可知,read()方法每次只能读取一个字节,所以也只能读取由ASCII码范围内的一些字符。这些字符主要用于显示现代英语和其他西欧语言。而对于汉字等unicode中的字符则不能正常读取。只能以乱码的形式显示。
对于read()方法的上述缺点,在read(byte[] b)中则得到了解决,就拿汉字来举例,一个汉字占有两个字节,则可以把参数数组b定义为大小为2的数组即可正常读取汉字了。当然b也可以定义为更大,比如如果b=new byte[4]的话,则每次可以读取两个汉字字符了,但是需要注意的是,如果此处定义b 的大小为3或7等奇数,则对于全是汉字的一篇文档则不能全部正常读写了。
read()方法读取的是一个字节,为什么返回的是int,而不是byte 因为字节输入流可以操作任意类型的文件,你如图片音频等,这些文件底层都是以二进制形式存储的,如果每次读取都返回byte,有可能在读到中间的时候遇到11111111
那么这11111111是byte类型的-1,我们的程序是遇到-1就会停止不读了,后面的数据就读不到了,所以在读取的时候用int类型接收,如果11111111会在其前面补上24个0凑足4个字节,那么byte类型的-1就变成int类型的255了,这样可以保证整个数据读完,而结束标记的-1就是int类型
1.1.下面用实例来演示
实例说明:演示read()方法的使用。主要任务是通过文件输入流FileInputStream来读取文本文档test.txt中的内容,并且输出到控制台上显示。
先看一下test.txt文档的内容:
生活就像海洋,只有意志坚强的人,才能到达彼岸。Life, just like the ocean,only the person who has strong faith, can get to the other shore。
生活就像海洋,只有意志坚强的人,才能到达彼岸。Life, just like the ocean,only the person who has strong faith, can get to the other shore。
测试代码如下:
File file = new File("D:/4/test.txt");
FileInputStream fis =null;
try {
fis = new FileInputStream(file);
int len;
StringBuffer stringBuffer = new StringBuffer();
byte[] bytes;
while ((len=fis.read())!=-1){
stringBuffer.append(String.valueOf(len));
}
System.out.println(stringBuffer.toString());
}catch (Exception e){
e.printStackTrace();
}finally {
if (fis!=null){
fis.close();
}
}
运行结果:
Éú»î¾ÍÏñº£Ñó£¬Ö»ÓÐÒâÖ¾¼áÇ¿µÄÈË£¬²ÅÄܵ½´ï±Ë°¶¡£Life, just like the ocean,only the person who has strong faith, can get to the other shore¡£
Éú»î¾ÍÏñº£Ñó£¬Ö»ÓÐÒâÖ¾¼áÇ¿µÄÈË£¬²ÅÄܵ½´ï±Ë°¶¡£Life, just like the ocean,only the person who has strong faith, can get to the other shore¡£
2.read(byte[] b)
read(byte[] b) : 从输入流中读取一定数量的字节,并将其存储在缓冲区数组 b 中。以整数形式返回实际读取的字节数。在输入数据可用、检测到文件末尾或者抛出异常前,此方法一直阻塞。如果 b 的长度为 0,则不读取任何字节并返回 0;否则,尝试读取至少一个字节。如果因为流位于文件末尾而没有可用的字节,则返回值 -1;否则,至少读取一个字节并将其存储在 b 中。将读取的第一个字节存储在元素 b[0] 中,下一个存储在 b[1] 中,依次类推。读取的字节数最多等于b 的长度。设 k 为实际读取的字节数;这些字节将存储在 b[0] 到 b[k-1] 的元素中,不影响 b[k] 到b[b.length-1] 的元素。
在read(byte[] b)中则得到了read()乱码的解决,就拿汉字来举例,一个汉字占有两个字节,则可以把参数数组b定义为大小为2的数组即可正常读取汉字了。当然b也可以定义为更大,比如如果b=new byte[4]的话,则每次可以读取两个汉字字符了,但是需要注意的是,如果此处定义b 的大小为3或7等奇数,则对于全是汉字的一篇文档则不能全部正常读写了。
2.1.下面用实例来演示
实例说明:演示read()方法的使用。主要任务是通过文件输入流FileInputStream来读取文本文档test.txt中的内容,并且输出到控制台上显示。
先看一下test.txt文档的内容:
生活就像海洋,只有意志坚强的人,才能到达彼岸。Life, just like the ocean,only the person who has strong faith, can get to the other shore。
生活就像海洋,只有意志坚强的人,才能到达彼岸。Life, just like the ocean,only the person who has strong faith, can get to the other shore。
测试代码如下:
File file = new File("D:/4/test.txt");
FileInputStream fis =null;
try {
fis = new FileInputStream(file);
int len;
StringBuffer stringBuffer = new StringBuffer();
byte[] bytes =new byte[2];
while ((len=fis.read(bytes))!=-1){
stringBuffer.append(new String(bytes,0,len,"GB2312"));
}
System.out.println(stringBuffer.toString());
}catch (Exception e){
e.printStackTrace();
}finally {
if (fis!=null){
fis.close();
}
}
运行结果:
生活就像海洋,只有意志坚强的人,才能到达彼岸。Life, just like the ocean,only the person who has strong faith, can get to the other shore。
生活就像海洋,只有意志坚强的人,才能到达彼岸。Life, just like the ocean,only the person who has strong faith, can get to the other shore。
3.read(byte[] b, int off, int len)
其实就是read(byte[] b)中读入固定长度字节到byte数组
3.1.下面用实例来演示
先看一下test.txt文档的内容:
生活就像海洋,只有意志坚强的人,才能到达彼岸。Life, just like the ocean,only the person who has strong faith, can get to the other shore。
生活就像海洋,只有意志坚强的人,才能到达彼岸。Life, just like the ocean,only the person who has strong faith, can get to the other shore。
测试代码如下:
File file = new File("D:/4/test.txt");
FileInputStream fis =null;
try {
fis = new FileInputStream(file);
int len;
StringBuffer stringBuffer = new StringBuffer();
byte[] bytes =new byte[12];
while ((len=fis.read(bytes,0,3))!=-1){ //截取三个字节,汉字会出现间隔乱码
stringBuffer.append(new String(bytes,0,len,"GB2312"));
}
System.out.println(stringBuffer.toString());
}catch (Exception e){
e.printStackTrace();
}finally {
if (fis!=null){
fis.close();
}
}
运行结果:
生�罹�像�Q�,�挥�意�炯�强�娜�,�拍�到�锉�岸��Life, just like the ocean,only the person who has strong faith, can get to the other shore。
��活就�窈�洋��只有�庵�坚�康�人��才能�酱�彼�丁�Life, just like the ocean,only the person who has strong faith, can get to the other shore。