管道流的主要作用可以用于两个线程之间的通信,有管道输出流 PipeOutputStream和管道输入流 PipeInputStream。然后通过connect将两个管道连接起来。

import java.io.PipedInputStream;
import java.io.PipedOutputStream; class Send implements Runnable{
private PipedOutputStream pos = null; public Send() {
// TODO Auto-generated constructor stub
pos = new PipedOutputStream();
} @Override
public void run() {
// TODO Auto-generated method stub
String str = "Hello world!";
try {
pos.write(str.getBytes());
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
} try {
pos.close();
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
} public PipedOutputStream getPos() {
return pos;
}
} class Receive implements Runnable{
private PipedInputStream pis = null; public Receive() {
// TODO Auto-generated constructor stub
pis = new PipedInputStream();
} public PipedInputStream getPis() {
return pis;
} @Override
public void run() {
// TODO Auto-generated method stub
byte[] b = new byte[1024];
int len = 0;
try {
len = pis.read(b);
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
} try {
pis.close();
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
} System.out.println(new String(b, 0, len));
} } public class PipeDemo01 { public static void main(String[] args) {
// TODO Auto-generated method stub
Send s = new Send();
Receive r = new Receive();
try {
s.getPos().connect(r.getPis());
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
} new Thread(s).start();
new Thread(r).start();
} }

  

Java管道流的更多相关文章

  1. Java管道流PipedStream

    管道读取流和管道写入流可以像管道一样对接上,管道读取流就可以读取管道写入流写入的数据.需要注意的是需要加入多线程技术,因为单线程,先执行read,会发生死锁,因为read方法是阻塞式的,没有数据的re ...

  2. Java管道流学习

    管道流 作用:用于线程之间的数据通信 管道流测试:一个线程写入,一个线程读取 import java.io.IOException; import java.io.PipedInputStream; ...

  3. java 管道流代码示例

    import java.io.IOException;import java.io.PipedInputStream;import java.io.PipedOutputStream; public ...

  4. java 管道流PipedInputStream,PipedInputStream和随机访问文件 RandomAccessFile

    http://blog.csdn.net/zlp1992/article/details/50298195   给个链接自己去看吧.网上资料不是很多,而且自己也不想写了 RandomAccessFil ...

  5. java:管道流(线程间管道流)

    class Send implements Runnable{ PipedOutputStream pos = null; public Send() { this.pos = new PipedOu ...

  6. JAVA IO之管道流总结大全(转)

    要在文本框中显示控制台输出,我们必须用某种方法“截取”控制台流.换句话说,我们要有一种高效地读取写入到System.out和 System.err 所有内容的方法.如果你熟悉Java的管道流Piped ...

  7. Java IO7:管道流、对象流

    前言 前面的文章主要讲了文件字符输入流FileWriter.文件字符输出流FileReader.文件字节输出流FileOutputStream.文件字节输入流FileInputStream,这些都是常 ...

  8. JAVA基础学习day22--IO流四-对象序列化、管道流、RandomAccessFile、DataStream、ByteArrayStream、转换流的字符编码

    一.对象序列化 1.1.对象序列化 被操作的对象需要实现Serializable接口 1.2.对象序列化流ObjectOutputStream与ObjectInputStream ObjectInpu ...

  9. Java:IO流其他类(字节数组流、字符数组流、数据流、打印流、Properities、对象流、管道流、随机访问、序列流、字符串读写流)

    一.字节数组流: 类 ByteArrayInputStream:在构造函数的时候,需要接受数据源,而且数据源是一个字节数组. 包含一个内部缓冲区,该缓冲区包含从流中读取的字节.内部计数器跟踪 read ...

随机推荐

  1. HDU 4974 Dracula and Ethan 优先队列

    Dracula and Ethan Time Limit: 1 Sec  Memory Limit: 256 MB Description Dragon is watching competition ...

  2. JNI NDK开发Crash错误定位 调试

    总结: 搜索backtrace  然后: $ /d/android-ndk-r10c/toolchains/arm-linux-androideabi-4.9/prebuilt/windows-x86 ...

  3. javascript操作cookies

    1.读取cookies getCookie: function(c_name){ if (document.cookie.length > 0) { var c_start = document ...

  4. LIS HDOJ 1257 最少拦截系统

    题目传送门 题意:中文题面 分析:LIS模板题:n - 最长下降子序列 -> 最长上升子序列 贪心做法以后再补:) 代码: #include <cstdio> #include &l ...

  5. 位运算 2013年山东省赛 F Alice and Bob

    题目传送门 /* 题意: 求(a0*x^(2^0)+1) * (a1 * x^(2^1)+1)*.......*(an-1 * x^(2^(n-1))+1) 式子中,x的p次方的系数 二进制位运算:p ...

  6. ConversionPattern 解析

    Sample <param name="ConversionPattern" value="%d [%t] %-5p %c [%x] %X{auth} - Line ...

  7. System call in linux by C

    1: #include <stdlib.h> 2: int system(const char *command); 3:  4: while (something) { 5: int r ...

  8. 洛谷 P1009 阶乘之和 Label:高精度

    题目描述 用高精度计算出S=1!+2!+3!+…+n!(n≤50) 其中“!”表示阶乘,例如:5!=5*4*3*2*1. 输入输出格式 输入格式: 一个正整数N. 输出格式: 一个正整数S,表示计算结 ...

  9. javascript中字符串常用操作总结、JS字符串操作大全

    字符串的操作在js中非常频繁,也非常重要.以往看完书之后都能记得非常清楚,但稍微隔一段时间不用,便会忘得差不多,记性不好是硬伤啊...今天就对字符串的一些常用操作做个整理,一者加深印象,二者方便今后温 ...

  10. hiho#1033 : 交错和

    描述 给定一个数 x,设它十进制展从高位到低位上的数位依次是 a0, a1, ..., an - 1,定义交错和函数: f(x) = a0 - a1 + a2 - ... + ( - 1)n - 1a ...