构造两个线程,通过管道将一个进程读入的文件从另外一个进程中输出到另外一个文件。
解决方案 »
- 100分求文中异常的解决方案-An unexpected error has been detected by Java Runtime Environment:
- JDBC连接数据库的问题
- 菜鸟救助TextField(30)中的代表什么意思?
- 有意思,看看为什么会输出这个结果???????????????
- 请问如何取得当前的年月?
- 我下了一个J2SDK1.4.2,怎样配置??
- 简单问题:何为mis和sap
- 关于ASCII码的转换问题
- 请问在java中怎样把“2”转换成int型?问题简单,不好意思了
- 请问 int compareTo(String other)怎么用啊
- 急!!!这个问题算法如何设计?
- java 获得windows的进程列表及其他?
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);
}
}
}