listing2.java
来自「控制台数据截取,(哎,还要打满20个字,麻烦)」· Java 代码 · 共 42 行
JAVA
42 行
import java.io.*;
public class Listing2 {
static PipedInputStream pipedIS = new PipedInputStream();
static PipedOutputStream pipedOS =
new PipedOutputStream();
public static void main(String[] a){
try {
pipedIS.connect(pipedOS);
}
catch(IOException e) {
System.err.println("连接失败");
System.exit(1);
}
byte[] inArray = new byte[10];
byte[] outArray = new byte[20];
int bytesRead = 0;
try {
// 向pipedOS发送20字节数据
pipedOS.write(outArray, 0, 20);
System.out.println(" 已发送20字节...");
// 在每一次循环迭代中,读入10字节
// 发送20字节
bytesRead = pipedIS.read(inArray, 0, 10);
int i=0;
while(bytesRead != -1) {
pipedOS.write(outArray, 0, 20);
System.out.println(" 已发送20字节..."+i);
i++;
bytesRead = pipedIS.read(inArray, 0, 10);
}
}
catch(IOException e) {
System.err.println("读取pipedIS时出现错误: " + e);
System.exit(1);
}
} // main()
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?