java学习之路----IO----内存操作流和管道流

本文探讨了在Java中使用内存流和管道流的技术,包括如何使用ByteArrayInputStream和ByteArrayOutputStream在内存中操作数据,以及如何通过管道流实现线程间的数据通信。实例演示了字符串大小写转换及多线程间信息传递的应用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

我们的流操作除了在文件上操作,还可以在内存中操作

                    用到的类
                    ByteArrayInputStream 主要完成把文件写入内存

                    ByteArrayOutputStream 主要将内存中内容输出

利用内存流来完成一个字符串大小写的转换

          public class ByteArrayDemo {
           public static void main(String[] args) throws Exception{
              String str= "HELLO WORLD";
              
              ByteArrayInputStream in= null;//内存输入流
              
              ByteArrayOutputStream out= null;//内存输出流
              
              in= new ByteArrayInputStream(str.getBytes());//向内存中输入内容
              
              out= new ByteArrayOutputStream();//准备输出
              
               int temp=0;
              
               while((temp=in.read())!=-1){
                    char c=(char )temp;
                   out.write(Character. toLowerCase(c));
              }
              String newStr=out.toString();
              in.close();
              out.close();
              System. out.println(newStr);
          }

}

结果:
hello world

内存流的作用一般是用来写临时文件的。



管道流的作用是用来进行两个线程之间的通信

          管道流分为输出流和输入流

                    如果要进行线程之间的通信,即必须要把输入流接到输出流上

     下面来验证一下管道流

class Send implements Runnable{

     private PipedOutputStream pip= null;//管道输出流
     
     
     public Send() {
           this.pip = new PipedOutputStream();
     }

     public void run() {
     String str= "hello world";
     
           try {
               this.pip .write(str.getBytes());//输出信息
          } catch (IOException e) {
               // TODO Auto-generated catch block
              e.printStackTrace();
          }
          
           try {
               this.pip .close();
          } catch (IOException e) {
               // TODO Auto-generated catch block
              e.printStackTrace();
          }
          
     }
     
     public PipedOutputStream getpip(){
           return pip ;
     }
}



class  Receive implements Runnable{

     PipedInputStream p= null;
     
     public Receive() {
           this.p = new PipedInputStream();
     }

     public void run() {
           byte b[]=new byte[1024];
           int len=0;
          
           try {
          len=  this.p .read(b);
          } catch (IOException e) {
               // TODO Auto-generated catch block
              e.printStackTrace();
          }
           try {
               p.close();
          } catch (IOException e) {
               // TODO Auto-generated catch block
              e.printStackTrace();
          }
          System. out.println("接收的内容是:" +new String(b,0,len));
     }
     
     public PipedInputStream getP(){
           return p ;
     }
     
}



public class PipDemo {
           public static void main(String[] args) {
              Send s= new Send();
              
              Receive r= new Receive();
              
          
              
               try {
                   s.getpip().connect(r.getP());      //接通管道
              } catch (IOException e) {
                    // TODO Auto-generated catch block
                   e.printStackTrace();
              }
              
               new Thread(s).start();
               new Thread(r).start();
          }

}


结果:
接收的内容是:hello world



               

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值