javaIO-管道简单使用以及底层原理
程序员文章站
2022-05-08 17:05:53
...
前言
今天来学习一下进程间的管道通信,管道有两种PipedInputStream,PipedOutputStream和PipedWriter和PipedReader
字节流
package thread;
import java.io.PipedOutputStream;
public class DataWriter extends Thread{
private PipedOutputStream out;
public DataWriter(PipedOutputStream out) {
this.out = out;
}
public void write() {
try {
String message = "123456";
System.out.println("开始写数据了");
out.write(message.getBytes());
out.close();
} catch (Exception e) {
// TODO: handle exception
}
}
@Override
public void run() {
write();
}
}
package thread;
import java.io.PipedInputStream;
import java.io.IOException;
public class DataReader extends Thread{
PipedInputStream input;
public DataReader(PipedInputStream input) {
this.input = input;
}
public void read() {
try {
System.out.println("开始读数据了");
byte[] read = new byte[20];
int num = input.read(read);
System.out.println(new String(read, 0, num));
input.close();
} catch (IOException e) {
}
}
@Override
public void run() {
read();
}
}
package thread;
import java.io.PipedInputStream;
import java.io.PipedOutputStream;
public class Test {
public static void main(String[] args) {
try {
PipedInputStream input = new PipedInputStream();
PipedOutputStream output = new PipedOutputStream();
DataReader reader = new DataReader(input);
DataWriter writer = new DataWriter(output);
input.connect(output);
reader.start();
writer.start();
} catch (Exception e) {
// TODO: handle exception
}
}
}
字符流
package thread;
import java.io.IOException;
import java.io.PipedReader;
public class DataReader extends Thread{
private PipedReader input;
public DataReader(PipedReader input) {
this.input = input;
}
public void read() {
try {
System.out.println("开始读数据了");
char[] read = new char[2048];
int num = input.read(read);
System.out.println(new String(read, 0, num));
input.close();
} catch (IOException e) {
}
}
@Override
public void run() {
read();
}
}
package thread;
import java.io.PipedWriter;
public class DataWriter extends Thread{
private PipedWriter out;
public DataWriter(PipedWriter out) {
this.out = out;
}
public void write() {
try {
String message = "123456";
System.out.println("开始写数据了");
out.write(message);
out.close();
} catch (Exception e) {
// TODO: handle exception
}
}
@Override
public void run() {
write();
}
}
package thread;
import java.io.PipedReader;
import java.io.PipedWriter;
public class Test {
public static void main(String[] args) {
try {
PipedWriter out = new PipedWriter();
PipedReader in = new PipedReader();
DataReader reader = new DataReader(in);
DataWriter writer = new DataWriter(out);
in.connect(out);
reader.start();
writer.start();
} catch (Exception e) {
// TODO: handle exception
}
}
}
讲解,底层实现
例子比较简单,关键是我们需要看看里面的一些原理,不用那么看源码,从我之前的经验来看看了也记不住。只需要看个大概就行了。
需要记住的是,输入和输出其实是共用一块数组存储空间,输出流的写操作用的其实是reader中的方法
管道中的写方法:
public void write(char cbuf[], int off, int len) throws IOException {
if (sink == null) {
throw new IOException("Pipe not connected");
} else if ((off | len | (off + len) | (cbuf.length - (off + len))) < 0) {
throw new IndexOutOfBoundsException();
}
sink.receive(cbuf, off, len);
}
reader的缓冲区,缓冲区由reader掌控
char buffer[];
reader中的receive方法,用来写入数据
synchronized void receive(char c[], int off, int len) throws IOException {
while (--len >= 0) {
receive(c[off++]);
}
}
知道这么多就足够了,细节部分不用深究。新手多学点实用的。
上一篇: windows系统cmd常用命令
下一篇: javaweb文件上传
推荐阅读