JavaIO流学习总结-PipedOutputStream和PipedInputStream基本操作练习
package io;
import java.io.IOException;
import java.io.PipedInputStream;
import java.io.PipedOutputStream;
import java.io.PipedInputStream;
import java.io.PipedOutputStream;
/**
* 修改日期:2020/03/31
* 修改人:牟松
* PipedOutputStream和PipedInputStream基本操作练习
* 修改日期:2020/03/31
* 修改人:牟松
* PipedOutputStream和PipedInputStream基本操作练习
* 管道流实现了两个线程之间的通信,多线程之间传输数据需要通信时可以用到管道流
* 参考连接:https://blog.csdn.net/qq_24434671/article/details/90206417
**/
public class PipdTest {
public static void main(String[] args) throws IOException {
// 创建一个发送者对象
Sender sender = new Sender();
// 创建一个接收者对象
Receiver receiver = new Receiver();
// 获取输出管道流
PipedOutputStream outputStream = sender.getOutputStream();
// 获取输入管道流
PipedInputStream inputStream = receiver.getInputStream();
// 链接两个管道,这一步很重要,把输入流和输出流联通起来
public class PipdTest {
public static void main(String[] args) throws IOException {
// 创建一个发送者对象
Sender sender = new Sender();
// 创建一个接收者对象
Receiver receiver = new Receiver();
// 获取输出管道流
PipedOutputStream outputStream = sender.getOutputStream();
// 获取输入管道流
PipedInputStream inputStream = receiver.getInputStream();
// 链接两个管道,这一步很重要,把输入流和输出流联通起来
outputStream.connect(inputStream);
// 启动发送者线程
sender.start();
// 启动接收者线程
receiver.start();
}
}
// 启动发送者线程
sender.start();
// 启动接收者线程
receiver.start();
}
}
/**
* 发送线程
* @author yuxuan
*/
class Sender extends Thread {
* 发送线程
* @author yuxuan
*/
class Sender extends Thread {
//声明一个 管道输出流对象 作为发送方
private PipedOutputStream outputStream = new PipedOutputStream();
public PipedOutputStream getOutputStream() {
return outputStream;
}
private PipedOutputStream outputStream = new PipedOutputStream();
public PipedOutputStream getOutputStream() {
return outputStream;
}
@Override
public void run() {
String msg = "Hello World";
try {
outputStream.write(msg.getBytes());
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
// 关闭输出流
outputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
public void run() {
String msg = "Hello World";
try {
outputStream.write(msg.getBytes());
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
// 关闭输出流
outputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
/**
* 接收线程
* @author yuxuan
*/
class Receiver extends Thread {
// 声明一个 管道输入对象 作为接收方
private PipedInputStream inputStream = new PipedInputStream();
public PipedInputStream getInputStream() {
return inputStream;
}
* 接收线程
* @author yuxuan
*/
class Receiver extends Thread {
// 声明一个 管道输入对象 作为接收方
private PipedInputStream inputStream = new PipedInputStream();
public PipedInputStream getInputStream() {
return inputStream;
}
@Override
public void run() {
public void run() {
byte[] buf = new byte[1024];
try {
// 通过read方法 读取长度
int len = inputStream.read(buf);
System.out.println(new String(buf, 0, len));
} catch (IOException e) {
e.printStackTrace();
int len = inputStream.read(buf);
System.out.println(new String(buf, 0, len));
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
// 关闭输入流
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
try {
// 关闭输入流
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}