简介:
PipedInputStream/PipedReader :管道输入流,主要在线程中使用 . 管道输入流是指一个通讯管道的接收端(Receiver)。
PipedOutputStream/PipedWriter :管道输出流,指在一个通讯管道的发送端(Sender)
主要应用:一个线程通过管道输出流发送数据,而另一个线程通过管道输入流读取数据,这样可实现两个线程间的通讯。
使用方法:
(1)在使用管道输出流和管道输入流时需要定义两个线程类(实现Runnable或继承Thread)
(2)在线程的发送端(Sender)内部,定义管道输出流:
public class Sender implements Runnable{
private PipedOutputStream pos;
//建立一个输出流对象通过发送端的构造函数传递
Sender(PipedOutputStream pos){
this.pos = pos;
}
@Override
public void run() {
//写入到输出流中
for(int i = 75;i < 90 ;i++){
try {
pos.write(i);
} catch (IOException e) {
e.printStackTrace();
}
}
try {
//写完记得关闭流对象
pos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
(3)在线程的接收端内部,定义管道输入流:
public class Receiver implements Runnable{
private PipedInputStream pis = null;
//建立构造函数传递一个管道输入流对象。
Receiver(PipedInputStream pis){
this.pis = pis;
}
//实现线程部分代码:
@Override
public void run() {
int len = 0;
//读取管道内的数据
try {
while((len = pis.read()) != -1){
System.out.println("Random: " + (char)len);
}
} catch (IOException e) {
e.printStackTrace();
}
try {
pis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
(4)在主线程中需要完成:1)管道流的实例对象的生成;2)管道流的连接;3)实例化线程的接收端和发送端并把管道流传递进去;4)开启线程。
public class Test {
public static void main(String[] args) throws IOException {
//分别创建输入流/输出流
PipedOutputStream pos = new PipedOutputStream();
PipedInputStream pis = new PipedInputStream();
//链接输入输出流
pos.connect(pis);
//实例化发送端接收端
Sender sender = new Sender(pos);
Receiver receiver = new Receiver(pis);
//创建并启动线程
new Thread(sender).start();;
new Thread(receiver).start();;
}
}
同样字符流应用在管道流中,下面就举一个列子利用管道流进行文件的复制:
接收端代码:线程的接收端-->管道输出端-->文件的输入端;
/**
* 从管道输入流中读取数据,利用文件输出流写到文件中
*/
public class Receiver implements Runnable {
// 建立字符管道输入流,文件输出流,
private PipedReader pr = null;
private FileWriter fw;
private File file;
// 构造参数接受一个管道输入流,和一个File对象作为输出文件的地址
Receiver(PipedReader pr, File file) {
this.pr = pr;
this.file = file;
}
// 实现线程部分代码:
@Override
public void run() {
try {
fw = new FileWriter(file);
} catch (IOException e) {
System.out.println("文件不存在");
}
// 读取管道内的数据,并写入文件输出流中
int data = 0;
try {
while ((data = pr.read()) != -1) {
fw.write((char) data);
fw.flush();
}
} catch (IOException e) {
e.printStackTrace();
}
//关闭流
try {
fw.close();
pr.close();
} catch (IOException e1) {
e1.printStackTrace();
}
}
}
发送端代码:线程的发送端-->管道输入端-->文件的输出端;
/**
* 从文件中读出数据进入文件输入流,并把文件输出流中的数据写入管道输出流中
*/
public class Sender implements Runnable {
private File file;
private FileReader fr;
private PipedWriter pw;
Sender(PipedWriter pw, File file) {
this.pw = pw;
this.file = file;
}
@Override
public void run() {
try {
fr = new FileReader(file);
int data = 0;
while ((data = fr.read()) != -1) {
pw.write((char) data);
pw.flush();
}
} catch (IOException e) {
e.printStackTrace();
}
try {
fr.close();
pw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
主线程代码:
public class Test {
public static void main(String[] args) throws IOException {
File R_file = new File("d://text.txt");
File S_file = new File("d://sc.txt");
//分别创建输入流/输出流
PipedReader pr = new PipedReader();
PipedWriter pw = new PipedWriter();
//链接输入输出流
pr.connect(pw);
//实例化发送端接收端
Sender sender = new Sender(pw,R_file);
Receiver receiver = new Receiver(pr,S_file);
//创建并启动线程
new Thread(sender).start();;
new Thread(receiver).start();;
}
}
可以看到:IO操作在实际应用的过程中可能会因为需求嵌套使用。具体什么时候该选择什么流对象,在IO最后会有总结;
网友评论