Wednesday, 31 August 2016

Java Tutorial : Java IO (ByteArrayOutputStream toByteArray method)


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

ByteArrayOutputStreamDemo.java
import java.io.ByteArrayOutputStream;
import java.io.IOException;

public class ByteArrayOutputStreamDemo
{
    /*
     * Created an instance of ByteArrayOutputStream and
     * wrote 5 random bytes into it. After that, I turned
     * the ByteArrayOutputStream instance into a byte array,
     * using the toByteArray() method, and then printed
     * every byte using a foreach loop.
     */

    public static void main(String[] args) throws IOException
    {
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();

        for (int i = 0; i < 5; i++)
        {
            byteArrayOutputStream.write((byte) (Math.random() * 100));
        }

        /*
         * Returns: 
         * -------- 
         * the current contents of this output stream, as a byte array.
         */
        byte[] byteArray = byteArrayOutputStream.toByteArray();
        for (byte b : byteArray)
        {
            System.out.print(b + " ");
        }
    }

}
Output
1 31 88 43 27 
Click the below link to download the code:
https://sites.google.com/site/ramj2eev1/home/javabasics/JavaIODemo_ByteArrayOS_toByteArray_method_App.zip?attredirects=0&d=1

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

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