字节流:
package com.wz.thread.stream;
import java.io.PipedOutputStream;
/**
* 字节输入流
* @author Administrator
*
*/
public class WriteData {
public void writeMethod(PipedOutputStream out) {
try {
System.out.println("write:");
for(int i = 0; i < 200; i++) {
String outData = "" + ( i + 1 );
out.write(outData.getBytes());
System.out.print(outData);
}
System.out.println();
out.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
package com.wz.thread.stream;
import java.io.PipedInputStream;
/**
* 字节输出流
* @author Administrator
*
*/
public class ReadData {
public void readMethod(PipedInputStream input) {
try {
System.out.println("read:");
byte[] byteArray = new byte[20];
int readLength = input.read(byteArray);
while(-1 != readLength) {
String newData = new String(byteArray, 0, readLength);
System.out.print(newData);
readLength = input.read(byteArray);
}
System.out.println();
input.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
package com.wz.thread.stream;
/**
* 写入流线程
* @author Administrator
*
*/
import java.io.PipedOutputStream;
public class ThreadWrite extends Thread{
private WriteData write;
private PipedOutputStream out;
public ThreadWrite(WriteData write, PipedOutputStream out) {
super();
this.write = write;
this.out = out;
}
@Override
public void run() {
super.run();
write.writeMethod(out);
}
}
package com.wz.thread.stream;
/**
* 读出流线程
* @author Administrator
*
*/
import java.io.PipedInputStream;
public class ThreadRead extends Thread{
private ReadData read;
private PipedInputStream input;
public ThreadRead(ReadData read, PipedInputStream input) {
super();
this.read = read;
this.input = input;
}
@Override
public void run() {
super.run();
read.readMethod(input);
}
}
package com.wz.thread.stream;
import java.io.PipedInputStream;
import java.io.PipedOutputStream;
/**
*
* @author Administrator
*
*/
public class Run {
public static void main(String[] args) {
try {
WriteData write = new WriteData();
ReadData read = new ReadData();
PipedOutputStream out = new PipedOutputStream();
PipedInputStream input = new PipedInputStream();
// 这个是相互建立连接
input.connect(out);
//out.connect(input);
ThreadWrite threadWrite = new ThreadWrite(write, out);
ThreadRead threadRead = new ThreadRead(read, input);
// 先读再写 更好的看出效果
threadRead.start();
Thread.sleep(2000);
threadWrite.start();
} catch (Exception e) {
e.printStackTrace();
}
}
}
字符流:
package com.wz.thread.readerandwrite;
import java.io.PipedWriter;
/**
* 字符输入流
* @author Administrator
*
*/
public class WriteData {
public void writeMethod(PipedWriter out) {
try {
System.out.println("write:");
for(int i = 0; i < 200; i++) {
String outData = "" + ( i + 1 );
out.write(outData);
System.out.print(outData);
}
System.out.println();
out.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
package com.wz.thread.readerandwrite;
import java.io.PipedReader;
/**
* 字符输出流
* @author Administrator
*
*/
public class ReadData {
public void readMethod(PipedReader input) {
try {
System.out.println("read:");
char[] byteArray = new char[20];
int readLength = input.read(byteArray);
while(-1 != readLength) {
String newData = new String(byteArray, 0, readLength);
System.out.print(newData);
readLength = input.read(byteArray);
}
System.out.println();
input.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
package com.wz.thread.readerandwrite;
/**
* 写入流线程
* @author Administrator
*
*/
import java.io.PipedWriter;
public class ThreadWrite extends Thread{
private WriteData write;
private PipedWriter out;
public ThreadWrite(WriteData write, PipedWriter out) {
super();
this.write = write;
this.out = out;
}
@Override
public void run() {
super.run();
write.writeMethod(out);
}
}
package com.wz.thread.readerandwrite;
/**
* 读出流线程
* @author Administrator
*
*/
import java.io.PipedReader;
public class ThreadRead extends Thread{
private ReadData read;
private PipedReader input;
public ThreadRead(ReadData read, PipedReader input) {
super();
this.read = read;
this.input = input;
}
@Override
public void run() {
super.run();
read.readMethod(input);
}
}
package com.wz.thread.readerandwrite;
import java.io.PipedReader;
import java.io.PipedWriter;
/**
*
* @author Administrator
*
*/
public class Run {
public static void main(String[] args) {
try {
WriteData write = new WriteData();
ReadData read = new ReadData();
PipedWriter out = new PipedWriter();
PipedReader input = new PipedReader();
// 这个是相互建立连接
input.connect(out);
//out.connect(input);
ThreadWrite threadWrite = new ThreadWrite(write, out);
ThreadRead threadRead = new ThreadRead(read, input);
// 先读再写 更好的看出效果
threadRead.start();
Thread.sleep(2000);
threadWrite.start();
} catch (Exception e) {
e.printStackTrace();
}
}
}