Tuesday 20 September 2016

Java Tutorial : Java IO (PipedReader and PipedWriter-V6)


Click here to watch in Youtube :
https://www.youtube.com/watch?v=kyfSpOHNBkA&list=UUhwKlOVR041tngjerWxVccw

PipeWriterThread.java
import java.io.PipedWriter;

public class PipeWriterThread implements Runnable
{
    private PipedWriter pw;
    private String name = null;

    public PipeWriterThread(String name, PipedWriter pw)
    {
        this.name = name;
        this.pw = pw;
    }

    public void run()
    {
        try
        {
            while (true)
            {
                // Write some data after every two seconds
                pw.write("Testing data written...\n");
                pw.flush();
                Thread.sleep(2000);
            }
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
    }

}
PipeReaderThread.java
import java.io.PipedReader;
 
public class PipeReaderThread implements Runnable
{
    private PipedReader pr;
    private String name = null;

    public PipeReaderThread(String name, PipedReader pr)
    {
        this.name = name;
        this.pr = pr;
    }

    public void run()
    {
        try
        {
            // continuously read data from stream and print
            // it in console
            while (true)
            {
                char c = (char) pr.read(); // read a char
                if (c != -1)
                { // check for -1 indicating end of file
                    System.out.print(c);
                }
            }
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
    }
}
PipedReadWriteDemo.java
import java.io.PipedReader;
import java.io.PipedWriter;

/*
 * Pipe streams are just like real plumbing pipes. You
 * put things into to a pipe at one end – using some
 * methods. Then you receive the same things back from
 * the pipe stream at the other end – using some other
 * methods. They come out in FIFO order, first-in
 * first-out, just like from real plumbing pipes.
 */

public class PipedReadWriteDemo
{

    public static void main(String[] args) throws Exception
    {
        try
        {
            // Create writer and reader instances
            PipedReader pr = new PipedReader();
            PipedWriter pw = new PipedWriter();
            // Connect the writer with reader
            pw.connect(pr);
            // Create one writer thread and one reader
            // thread
            Thread thread1 = new Thread(
                    new PipeReaderThread("ReaderThread", pr));
            Thread thread2 = new Thread(
                    new PipeWriterThread("WriterThread", pw));
            // start both threads
            thread1.start();
            thread2.start();
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
    }
}
Output
Testing data written...
Testing data written...
Testing data written...
Testing data written...
Testing data written...

Click the below link to download the code:
https://sites.google.com/site/ramj2eev1/home/javabasics/JavaIODemo_PipedReader_PipedReader_V6_App.zip?attredirects=0&d=1

Github Link:
https://github.com/ramram43210/Java/tree/master/BasicJava/JavaIODemo_PipedReader_PipedReader_V6_App

Bitbucket Link:
https://bitbucket.org/ramram43210/java/src/301d989d72a8b053f20342cfe0bd4a5c143c23aa/BasicJava/JavaIODemo_PipedReader_PipedReader_V6_App/?at=master

See also:
  • All JavaEE Viedos Playlist
  • All JavaEE Viedos
  • All JAVA EE Links
  • Servlets Tutorial
  • All Design Patterns Links
  • JDBC Tutorial
  • Java Collection Framework Tutorial
  • JAVA Tutorial
  • Kids Tutorial
  • No comments:

    Post a Comment