InputStream中read()与read(byte[] b)
发布日期:2021-07-19 12:29:31 浏览次数:9 分类:技术文章

本文共 7582 字,大约阅读时间需要 25 分钟。

read()与read(byte[] b)这两个方法在抽象类InputStream中前者是作为抽象方法存在的,后者不是,JDK API中是这样描述两者的: 

1:read() : 
从输入流中读取数据的下一个字节,返回0到255范围内的int字节值。如果因为已经到达流末尾而没有可用的字节,则返回-1。在输入数据可用、检测到流末尾或者抛出异常前,此方法一直阻塞。 

2: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()方法每次只能读取一个字节,所以也只能读取由ASCII码范围内的一些字符。这些字符主要用于显示现代英语和其他西欧语言。而对于汉字等unicode中的字符则不能正常读取。只能以乱码的形式显示。 

对于read()方法的上述缺点,在read(byte[] b)中则得到了解决,就拿汉字来举例,一个汉字占有两个字节,则可以把参数数组b定义为大小为2的数组即可正常读取汉字了。当然b也可以定义为更大,比如如果b=new byte[4]的话,则每次可以读取两个汉字字符了,但是需要注意的是,如果此处定义b 的大小为3或7等奇数,则对于全是汉字的一篇文档则不能全部正常读写了。 

下面用实例来演示一下二者的用法: 
实例说明:类InputStreamTest1.java 来演示read()方法的使用。类InputStreamTest2.java来演示read(byte[] b)的使用。两个类的主要任务都是通过文件输入流FileInputStream来读取文本文档xuzhimo.txt中的内容,并且输出到控制台上显示。 

先看一下xuzhimo.txt文档的内容 



InputStreamTest1.java 

Java代码   
  1. /**  
  2.  * User: liuwentao  
  3.  * Time: 12-1-25 上午10:11  
  4.  */  
  5. public class InputStreamTest1 {   
  6.     public static void main(String[] args){   
  7.         String path = "D:\\project\\opensouce\\opensouce_demo\\base_java\\classes\\demo\\java\\inputstream\\";   
  8.         File file = new File(path + "xuzhimo.txt");   
  9.         InputStream inputStream = null;   
  10.         int i=0;   
  11.         try {   
  12.             inputStream = new FileInputStream(file);   
  13.             while ((i = inputStream.read())!=-1){   
  14.                 System.out.print((char)i + "");   
  15.             }   
  16.         }catch (FileNotFoundException e) {   
  17.             e.printStackTrace();   
  18.         } catch (IOException e) {   
  19.             e.printStackTrace();   
  20.         }   
  21.     }   
  22. }  
[java] 
  1. /** 
  2.  * User: liuwentao 
  3.  * Time: 12-1-25 上午10:11 
  4.  */  
  5. public class InputStreamTest1 {  
  6.     public static void main(String[] args){  
  7.         String path = "D:\\project\\opensouce\\opensouce_demo\\base_java\\classes\\demo\\java\\inputstream\\";  
  8.         File file = new File(path + "xuzhimo.txt");  
  9.         InputStream inputStream = null;  
  10.         int i=0;  
  11.         try {  
  12.             inputStream = new FileInputStream(file);  
  13.             while ((i = inputStream.read())!=-1){  
  14.                 System.out.print((char)i + "");  
  15.             }  
  16.         }catch (FileNotFoundException e) {  
  17.             e.printStackTrace();  
  18.         } catch (IOException e) {  
  19.             e.printStackTrace();  
  20.         }  
  21.     }  
  22. }  

执行结果: 




如果将while循环中的 (char)去掉,即改成: 

引用
System.out.print(i + "");

则执行结果: 




InputStreamTest2.java
 

Java代码   
  1. /**  
  2.  * User: liuwentao  
  3.  * Time: 12-1-25 上午10:11  
  4.  */  
  5. public class InputStreamTest2 {   
  6.     public static void main(String[] args){   
  7.         String path = "D:\\project\\opensouce\\opensouce_demo\\base_java\\src\\demo\\java\\inputstream\\";   
  8.         File file = new File(path + "xuzhimo.txt");   
  9.         InputStream inputStream = null;   
  10.         int i=0;   
  11.         try {   
  12.             inputStream = new FileInputStream(file);   
  13.             byte[] bytes = new byte[16];   
  14.             while ((i = inputStream.read(bytes))!=-1){   
  15.                 String str = new String(bytes);   
  16.                 System.out.print(str);   
  17.             }   
  18.         }catch (FileNotFoundException e) {   
  19.             e.printStackTrace();   
  20.         } catch (IOException e) {   
  21.             e.printStackTrace();   
  22.         }   
  23.     }   
  24. }  
[java] 
  1. /** 
  2.  * User: liuwentao 
  3.  * Time: 12-1-25 上午10:11 
  4.  */  
  5. public class InputStreamTest2 {  
  6.     public static void main(String[] args){  
  7.         String path = "D:\\project\\opensouce\\opensouce_demo\\base_java\\src\\demo\\java\\inputstream\\";  
  8.         File file = new File(path + "xuzhimo.txt");  
  9.         InputStream inputStream = null;  
  10.         int i=0;  
  11.         try {  
  12.             inputStream = new FileInputStream(file);  
  13.             byte[] bytes = new byte[16];  
  14.             while ((i = inputStream.read(bytes))!=-1){  
  15.                 String str = new String(bytes);  
  16.                 System.out.print(str);  
  17.             }  
  18.         }catch (FileNotFoundException e) {  
  19.             e.printStackTrace();  
  20.         } catch (IOException e) {  
  21.             e.printStackTrace();  
  22.         }  
  23.     }  
  24. }  

执行结果: 


 


遗憾的是,还是有乱码,解决办法可以参见下面教程 

 


修改后的代码: 

Java代码   
  1. /**  
  2.  * User: liuwentao  
  3.  * Time: 12-1-25 上午10:11  
  4.  */  
  5. public class InputStreamTest3 {   
  6.     public static void main(String[] args) {   
  7.         String path = "D:\\project\\opensouce\\opensouce_demo\\base_java\\src\\demo\\java\\inputstream\\";   
  8.         File file = new File(path + "xuzhimo.txt");   
  9.         InputStream inputStream = null;   
  10.         String line;   
  11.         StringBuffer stringBuffer = new StringBuffer();   
  12.         try {   
  13.             //InputStream :1)抽象类,2)面向字节形式的I/O操作(8 位字节流) 。   
  14.             inputStream = new FileInputStream(file);   
  15.             //Reader :1)抽象类,2)面向字符的 I/O操作(16 位的Unicode字符) 。   
  16.             Reader reader = new InputStreamReader(inputStream, "UTF-8");   
  17.             //增加缓冲功能   
  18.             BufferedReader bufferedReader = new BufferedReader(reader);   
  19.             while ((line = bufferedReader.readLine()) != null) {   
  20.                 stringBuffer.append(line);   
  21.             }   
  22.             if (bufferedReader != null) {   
  23.                 bufferedReader.close();   
  24.             }   
  25.             String content = stringBuffer.toString();   
  26.             System.out.print(content);   
  27.         } catch (FileNotFoundException e) {   
  28.             e.printStackTrace();   
  29.         } catch (IOException e) {   
  30.             e.printStackTrace();   
  31.         }   
  32.     }   
  33. }  
[java] 
  1. /** 
  2.  * User: liuwentao 
  3.  * Time: 12-1-25 上午10:11 
  4.  */  
  5. public class InputStreamTest3 {  
  6.     public static void main(String[] args) {  
  7.         String path = "D:\\project\\opensouce\\opensouce_demo\\base_java\\src\\demo\\java\\inputstream\\";  
  8.         File file = new File(path + "xuzhimo.txt");  
  9.         InputStream inputStream = null;  
  10.         String line;  
  11.         StringBuffer stringBuffer = new StringBuffer();  
  12.         try {  
  13.             //InputStream :1)抽象类,2)面向字节形式的I/O操作(8 位字节流) 。  
  14.             inputStream = new FileInputStream(file);  
  15.             //Reader :1)抽象类,2)面向字符的 I/O操作(16 位的Unicode字符) 。  
  16.             Reader reader = new InputStreamReader(inputStream, "UTF-8");  
  17.             //增加缓冲功能  
  18.             BufferedReader bufferedReader = new BufferedReader(reader);  
  19.             while ((line = bufferedReader.readLine()) != null) {  
  20.                 stringBuffer.append(line);  
  21.             }  
  22.             if (bufferedReader != null) {  
  23.                 bufferedReader.close();  
  24.             }  
  25.             String content = stringBuffer.toString();  
  26.             System.out.print(content);  
  27.         } catch (FileNotFoundException e) {  
  28.             e.printStackTrace();  
  29.         } catch (IOException e) {  
  30.             e.printStackTrace();  
  31.         }  
  32.     }  
  33. }  

执行结果: 




还是遗憾,没有换行。 


解决办法,通过 commons-io-*.jar 

Java代码   
  1. /**  
  2.  * User: liuwentao  
  3.  * Time: 12-1-25 上午10:11  
  4.  */  
  5. public class InputStreamTest4 {   
  6.     public static void main(String[] args) {   
  7.         String path = "D:\\project\\opensouce\\opensouce_demo\\base_java\\src\\demo\\java\\inputstream\\";   
  8.         File file = new File(path + "xuzhimo.txt");   
  9.   
  10.         String content = null;   
  11.         try {   
  12.             content = FileUtils.readFileToString(file, "utf-8");   
  13.         } catch (IOException e) {   
  14.             e.printStackTrace();   
  15.         }   
  16.         System.out.println("content:" + content);   
  17.     }   
  18. }  
[java] 
  1. /** 
  2.  * User: liuwentao 
  3.  * Time: 12-1-25 上午10:11 
  4.  */  
  5. public class InputStreamTest4 {  
  6.     public static void main(String[] args) {  
  7.         String path = "D:\\project\\opensouce\\opensouce_demo\\base_java\\src\\demo\\java\\inputstream\\";  
  8.         File file = new File(path + "xuzhimo.txt");  
  9.   
  10.         String content = null;  
  11.         try {  
  12.             content = FileUtils.readFileToString(file, "utf-8");  
  13.         } catch (IOException e) {  
  14.             e.printStackTrace();  
  15.         }  
  16.         System.out.println("content:" + content);  
  17.     }  
  18. }  

执行结果: 


 

 

1. 关于InputStream.read()
     在从数据流里读取数据时,为图简单,经常用InputStream.read()方法。这个方法是从流里每次只读取读取一个字节,效率会非常低。     更好的方法是用InputStream.read(byte[] b)或者InputStream.read(byte[] b,int off,int len)方法,一次读取多个字节。


2. 关于InputStream类的available()方法
    要一次读取多个字节时,经常用到InputStream.available()方法,这个方法可以在读写操作前先得知数据流里有多少个字节可以读取。需要注意的是,如果这个方法用在从本
地文件读取数据时,一般不会遇到问题,但如果是用于网络操作,就经常会遇到一些麻烦。比如,Socket通讯时,对方明明发来了1000个字节,但是自己的程序调用available()方法却只得到900,或者100,甚至是0,感觉有点莫名其妙,怎么也找不到原因。其实,这是因为网络通讯往往是间断性的,一串字节往往分几批进行发送。本地程序调用available()方法有时得到0,这可能是对方还没有响应,也可能是对方已经响应了,但是数据还没有送达本地。对方发送了1000个字节给你,也许分成3批到达,这你就要调用3次available()方法才能将数据总数全部得到。
      如果这样写代码:
  int count = in.available();
  byte[] b = new byte[count];
  in.read(b);
      在进行网络操作时往往出错,因为你调用available()方法时,对发发送的数据可能还没有到达,你得到的count是0。
         需要改成这样:
  int count = 0;
  while (count == 0) {


   count = in.available();
  }
  byte[] b = new byte[count];
  in.read(b);
3. 关于InputStream.read(byte[] b)和InputStream.read(byte[] b,int off,int len)这两个方法都是用来从流里读取多个字节的,有经验的程序员就会发现,这两个方法经常 读取不到自己想要读取的个数的字节。比如第一个方法,程序员往往希望程序能读取到b.length个字节,而实际情况是,系统往往读取不了这么多。仔细阅读Java的API说明就发现了,这个方法 并不保证能读取这么多个字节,它只能保证最多读取这么多个字节(最少1个)。因此,如果要让程序读取count个字节,最好用以下代码:
  byte[] b = new byte[count];
  int readCount = 0; // 已经成功读取的字节的个数
  while (readCount < count) {

   readCount += in.read(bytes, readCount, count - readCount);
  }
      用这段代码可以保证读取count个字节,除非中途遇到IO异常或者到了数据流的结尾(EOFException)

转载地址:https://blog.csdn.net/jdsjlzx/article/details/8875758 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:Android引入外部自定义特殊字体的方法
下一篇:DatagramSocket.receive()方法无响应的解决

发表评论

最新留言

留言是一种美德,欢迎回访!
[***.207.175.100]2024年03月26日 07时10分40秒