Tuesday, 20 September 2016

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


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

Click the below Image to Enlarge
Java Tutorial : Java IO (PipedReader and PipedWriter-V4) 
Java Tutorial : Java IO (PipedReader and PipedWriter-V4) 
PipedReadWriteDemo.java
import java.io.IOException;
import java.io.PipedReader;
import java.io.PipedWriter;

public class PipedReadWriteDemo
{

    public static void main(String[] args) throws IOException
    {
        PipedReader pr = new PipedReader();
        PipedWriter pw = new PipedWriter();
        pw.connect(pr);

        Runnable producer = () -> produceData(pw);
        Runnable consumer = () -> consumeData(pr);
        new Thread(producer).start();
        new Thread(consumer).start();
    }

    public static void produceData(PipedWriter pw)
    {
        try
        {
            for (int i = 1; i <= 5; i++)
            {
                pw.write("Welcome " + i+"\n");
                pw.flush();
                Thread.sleep(500);
            }
            pw.close();
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
    }

    public static void consumeData(PipedReader pr)
    {
        try
        {
            int num = -1;
            while ((num = pr.read()) != -1)
            {
                System.out.print((char) num);
            }
            pr.close();
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }

    }
}
Output
Welcome 1
Welcome 2
Welcome 3
Welcome 4
Welcome 5

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

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

Bitbucket Link:
https://bitbucket.org/ramram43210/java/src/301d989d72a8b053f20342cfe0bd4a5c143c23aa/BasicJava/JavaIODemo_PipedReader_PipedReader_V4_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

    Google+ Followers

    Google+ Badge