public class Demo {
//通过管道完成文件的拷贝
public static void main(String[] arg) {
PipedInputStream in =new PipedInputStream();
PipedOutputStream out;
try {
out = new PipedOutputStream(in);
sender s =new sender(out);
receiver r =new receiver(in);
ExecutorService service =Executors.newCachedThreadPool();
service.execute(s);
service.execute(r);
service.shutdown();
service.awaitTermination(1, TimeUnit.DAYS);
} catch (Exception e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
} }
class sender implements Runnable{
PipedOutputStream out =null;
sender(PipedOutputStream temp){
out =temp; }
@Override public void run() {
// TODO 自动生成的方法存根
File file = new File("D:\\aa.log");
FileInputStream inStream;
try {
inStream = new FileInputStream(file);
byte[] buff=new byte[1024];
int len =0;
while((len = inStream.read(buff))!=-1) {
out.write(buff,0,len);
}
out.close();
inStream.close();
System.out.println("完成文件读取");
} catch (Exception e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
}
}
class receiver implements Runnable{
PipedInputStream in;
public receiver(PipedInputStream temp) {
in = temp;
}
@Override public void run() {
// TODO 自动生成的方法存根
try {
FileOutputStream outStream =new FileOutputStream("F:\\kk.log");
byte[] buff =new byte[1024];
int len=0;
while((len=in.read(buff))!=-1) {
outStream.write(buff, 0, len);
outStream.flush();
}
outStream.close();
in.close();
System.out.println("完成新文件写入");
} catch (Exception e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
} } }