轻松

记述我学习java的里程

   :: 首页 ::  :: 联系 ::  :: 管理 ::
  56 随笔 :: 0 文章 :: 69 评论 :: 0 Trackbacks

import java.io.*;

public class PipeIODemo1{
 public static void main(String[] args) throws IOException{
  //创建一个管道输出流对象
  PipedWriter out=new PipedWriter();
  
  //创建一个管道输入流对象
  PipedReader in=new PipedReader();
  //把管道输入流对象和管道输出流对象联接起来
  in.connect(out);
  
  //以上2个语句等效于
  //PipedReader in=new PipedReader(out);
  
        OutThread objOut=new OutThread(out);
  InThread objIn=new InThread(in);
  objOut.start();
  objIn.start();

  try{
   objOut.join();
   objIn.join();
  }catch (InterruptedException e){}

        System.out.println();
  System.out.println("Run Completed!!");
 }
}


//定义一个写线程类
class OutThread extends Thread{
 private Writer out;
 
 public OutThread(Writer out){
  this.out=out;
 }
 
 public void run(){
  try{
   try{
    for(char c='A'; c<='Z'; c++)
     out.write(c);
   }finally{
    out.close();
   }
  }catch(IOException e){
   getThreadGroup().uncaughtException(this, e);
  }
 }
}

class InThread extends Thread{
 private Reader in;

 public InThread(Reader in){
  this.in=in;
 }

 public void run(){
  int ch;
  try{
   try{
    while ((ch=in.read())!=-1)
     System.out.print((char)ch);
   }finally{
    in.close();
   }
  }catch(IOException e){
   getThreadGroup().uncaughtException(this, e);
  }
 }
}

posted on 2005-12-02 09:05 轻松 阅读(505) 评论(0)  编辑  收藏 所属分类: JAVA转贴

标题  
姓名  
主页
验证码 *  
内容(请不要发表任何与政治相关的内容)  
  登录  使用高级评论  新用户注册  返回页首  恢复上次提交      
 
 
相关链接:
网站导航: