Java IO-pipe stream

Source: Internet
Author: User

Import java. io. *; class Send implements Runnable {// Thread class private PipedOutputStream pos = null; // The public Send () {this. pos = new PipedOutputStream (); // instantiate the output stream} public void run () {String str = "Hello World !!! "; // Try {this. pos. write (str. getBytes ();} catch (IOException e) {e. printStackTrace ();} try {this. pos. close ();} catch (IOException e) {e. printStackTrace () ;}} public PipedOutputStream getPos () {// gets the output stream of this thread's pipeline return this. pos ;}}; class Receive implements Runnable {private PipedInputStream pis = null; // The public Receive () {this. pis = new PipedInputStream (); // instantiate input stream} public void run () {byte B [] = new byte [1024]; // receive content int len = 0; try {len = this. pis. read (B); // read content} catch (IOException e) {e. printStackTrace ();} try {this. pis. close (); // close} catch (IOException e) {e. printStackTrace ();} System. out. println ("received content:" + new String (B, 0, len);} public PipedInputStream getPis () {return this. pis ;}}; public class PipedDemo {public static void main (String args []) {Send s = new Send (); Receive r = new Receive (); try {s. getPos (). connect (r. getPis (); // connection pipe} catch (IOException e) {e. printStackTrace ();} new Thread (s ). start (); // start the Thread new Thread (r ). start (); // start thread }};

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.