通过管道实现文件的拷贝
Posted 愚钝的Tom
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了通过管道实现文件的拷贝相关的知识,希望对你有一定的参考价值。
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();
} } }
以上是关于通过管道实现文件的拷贝的主要内容,如果未能解决你的问题,请参考以下文章