下面是用文件输入输出流配合管道输入输出流完成文件复制的功能。
发送方:文件输入流读数据到管道输出流。
接收方:管道输入流读数据到文件输出流。
package pipedCommunication;
import java.io.*;
public class Demo {
//发送线程
static class Sender implements Runnable {
PipedOutputStream pipedOutputStream = null;
public Sender(PipedOutputStream pipedOutputStream) {
this.pipedOutputStream = pipedOutputStream;
}
@Override
public void run() {
int len;
byte[] b = new byte[4];
try (BufferedInputStream bis = new BufferedInputStream(
new FileInputStream("C:\\Users\\PUBG\\Desktop\\test.txt"));) { //这里可以改为从控制台读入
while ((len = bis.read(b)) != -1) {
pipedOutputStream.write(b, 0, len);
}
pipedOutputStream.close();
} catc