//使用管道流和多线程完成以下任务要求:
//一个线程对象t1 会从控制台中不停的读取信息,读到之后把数据交给管道输出流,把数据写到管道里面
//另一个线程对象t2 不停的使用管道输入流从管道里面读取数据,再把读到的数据写到一个文件里面
package day1411;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PipedInputStream;
import java.io.PipedOutputStream;
public class demo {
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
PipedInputStream in=new PipedInputStream();
PipedOutputStream out=new PipedOutputStream(in);
Thread t1=new PipedReadThread(in);
Thread t2=new PipedWriteThread(out);
t1.start();
t2.start();
}}
class PipedReadThread extends Thread{
private PipedInputStream in;
public PipedReadThread(PipedInputStream in) {
// TODO Auto-generated constructor stub
this.in=in;
}
public void run(){
OutputStream out = null;
String filePath = "src/day1411/c.txt";
try {
out = new FileOutputStream(filePath,true);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
int len = -1;
try {
while((len=this.in.read())!=-1){
out.write(len);
out.flush();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}}}
class PipedWriteThread extends Thread{
private PipedOutputStream out;
public PipedWriteThread(PipedOutputStream out) {
this.out = out;
}
@Override
public void run() {
try {
while(true){
InputStream in = null;
in=System.in;
int i=in.read();
out.write(i);
out.flush();}
} catch (Exception e) {
e.printStackTrace();
}finally {
try { if(out!=null)out.close();}
catch (IOException e) {
e.printStackTrace();
}}}}
//一个线程对象t1 会从控制台中不停的读取信息,读到之后把数据交给管道输出流,把数据写到管道里面
//另一个线程对象t2 不停的使用管道输入流从管道里面读取数据,再把读到的数据写到一个文件里面
package day1411;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PipedInputStream;
import java.io.PipedOutputStream;
public class demo {
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
PipedInputStream in=new PipedInputStream();
PipedOutputStream out=new PipedOutputStream(in);
Thread t1=new PipedReadThread(in);
Thread t2=new PipedWriteThread(out);
t1.start();
t2.start();
}}
class PipedReadThread extends Thread{
private PipedInputStream in;
public PipedReadThread(PipedInputStream in) {
// TODO Auto-generated constructor stub
this.in=in;
}
public void run(){
OutputStream out = null;
String filePath = "src/day1411/c.txt";
try {
out = new FileOutputStream(filePath,true);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
int len = -1;
try {
while((len=this.in.read())!=-1){
out.write(len);
out.flush();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}}}
class PipedWriteThread extends Thread{
private PipedOutputStream out;
public PipedWriteThread(PipedOutputStream out) {
this.out = out;
}
@Override
public void run() {
try {
while(true){
InputStream in = null;
in=System.in;
int i=in.read();
out.write(i);
out.flush();}
} catch (Exception e) {
e.printStackTrace();
}finally {
try { if(out!=null)out.close();}
catch (IOException e) {
e.printStackTrace();
}}}}