java字节输入流

学习了一下Java的字节输入流,下面是其使用入门的几个小例子。

Java代码 复制代码
  1. import java.io.ByteArrayInputStream;  
  2. import java.io.FileInputStream;  
  3. import java.io.FileNotFoundException;  
  4. import java.io.IOException;  
  5.   
  6. public class inputStreamTest   
  7. {  
  8.     public static void main(String[] args)  
  9.      {  
  10.          byteArrayInputStream();  
  11.           
  12.          fileInputStreamTest();  
  13.      }  
  14.       
  15.     public static void byteArrayInputStream()  
  16.      {  
  17.         byte[] buffer = new byte[]{3, -1, 36, -9, 20};  
  18.          ByteArrayInputStream in = new ByteArrayInputStream(buffer);  
  19.           
  20. //       int data = in.read();  
  21. //       while(data != -1)  
  22. //       {  
  23.             //输出结果为:3   255   36   247   20  
  24. //           System.out.print(data + "   ");      
  25. //           data = in.read();  
  26. //       }  
  27.   
  28.         byte[] buff = new byte[buffer.length];  
  29.         try  
  30.          {  
  31.              in.read(buff);  
  32.             //(需要将前面的部分注释掉)输出结果为:3   -1   36   -9   20   
  33.             for(int i=0; i<buff.length; i++)  
  34.                  System.out.print(buff[i] + "   ");   
  35.               
  36.          } catch(IOException e)  
  37.          {  
  38.              e.printStackTrace();  
  39.          }  
  40.           
  41.         try   
  42.          {  
  43.              in.close();  
  44.               
  45.          } catch (IOException e) {  
  46.              e.printStackTrace();  
  47.          }  
  48.      }  
  49.       
  50.     public static void fileInputStreamTest()  
  51.      {  
  52.          System.out.println();  
  53.         try   
  54.          {  
  55.             //a.txt中内容为:abc中国def  
  56.              FileInputStream is = new FileInputStream("C:\\a.txt");  
  57.               
  58.             int data = is.read();  
  59.               
  60.             //输出结果为:97   98   99   214   208   185   250   100   101   102  
  61.             while(data != -1)  
  62.              {  
  63.                  System.out.print(data + "   ");    
  64.                  data = is.read();  
  65.              }  
  66.               
  67.              is.close();  
  68.               
  69.          } catch (FileNotFoundException e) {  
  70.              e.printStackTrace();  
  71.          } catch (IOException e) {  
  72.              e.printStackTrace();  
  73.          }  
  74.      }  
  75. }  
import java.io.ByteArrayInputStream; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; public class inputStreamTest { public static void main(String[] args) { byteArrayInputStream(); fileInputStreamTest(); } public static void byteArrayInputStream() { byte[] buffer = new byte[]{3, -1, 36, -9, 20}; ByteArrayInputStream in = new ByteArrayInputStream(buffer); // int data = in.read(); // while(data != -1) // { //输出结果为:3 255 36 247 20 // System.out.print(data + " "); // data = in.read(); // } byte[] buff = new byte[buffer.length]; try { in.read(buff); //(需要将前面的部分注释掉)输出结果为:3 -1 36 -9 20 for(int i=0; i<buff.length; i++) System.out.print(buff[i] + " "); } catch(IOException e) { e.printStackTrace(); } try { in.close(); } catch (IOException e) { e.printStackTrace(); } } public static void fileInputStreamTest() { System.out.println(); try { //a.txt中内容为:abc中国def FileInputStream is = new FileInputStream("C:\\a.txt"); int data = is.read(); //输出结果为:97 98 99 214 208 185 250 100 101 102 while(data != -1) { System.out.print(data + " "); data = is.read(); } is.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } }

     有两点需要解释一下:

     对字节数组输入流( ByteArrayInputStream),对于字节类型的-9,二进制为11110111,转换为int类型(int data = in.read();)的二进制形式为00000000 00000000 00000000 11110111,因此字节类型的-9转换为int类型的247,也就会输出247. 对于-1,也是类似的情况,转换为了255.

      对文件输入流(FileInputStream),字符"a"、"b"、"c"的GBK编码各占1个字节,分别是97、98、99,而"中"和"国"的 GBK编码各占2个字节,分别是214和208,以及185和250,所以才会有上面的输出结果。当然,也可以用read(byte[] buff)方法来读取文件以提高效率。

posted on 2008-12-30 16:37  dhj  阅读(129)  评论(0编辑  收藏  举报

导航