分享

将输出流(OutputStream)转化为输入流(InputStream)的方法(一)

 CevenCheng 2010-11-04


 

Java代码 
  1.    
  2. package test.io;  
  3. import java.io.ByteArrayInputStream;  
  4. import java.io.ByteArrayOutputStream;  
  5. import java.io.IOException;  
  6. /** 
  7.  * 用于把OutputStream 转化为 InputStream。 
  8.  * 适合于数据量不大,且内存足够全部容纳这些数据的情况。 
  9.  * @author 赵学庆 www.java2000.net 
  10.  * 
  11.  */  
  12. public class Test1 {  
  13.   /** 
  14.    * @param args 
  15.    * @throws IOException 
  16.    */  
  17.   public static void main(String[] args) throws IOException {  
  18.     ByteArrayOutputStream out = new ByteArrayOutputStream();  
  19.     OutputStreamClass1.putDataOnOutputStream(out);  
  20.     InputStreamClass1.processDataFromInputStream(new ByteArrayInputStream(out.toByteArray()));  
  21.   }  
  22. }  
  23. class OutputStreamClass1 {  
  24.   public static void putDataOnOutputStream(ByteArrayOutputStream out) throws IOException {  
  25.     byte[] bs = new byte[] { 12345 };  
  26.     out.write(bs);  
  27.   }  
  28. }  
  29. class InputStreamClass1 {  
  30.   public static void processDataFromInputStream(ByteArrayInputStream in) throws IOException {  
  31.     byte[] bs = new byte[1024];  
  32.     int len = in.read(bs);  
  33.     for (int i = 0; i < len; i++) {  
  34.       System.out.println(bs[i]);  
  35.     }  
  36.   }  
  37. }  

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多