Wednesday, 31 August 2016

Java Tutorial : Java IO (ByteArrayInputStream reset method)


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

ByteArrayInputStreamDemo.java
import java.io.ByteArrayInputStream;
import java.io.IOException;

/*
 * This example first reads each character from the
 * stream and prints it as is, in lowercase. It then
 * resets the stream and begins reading again, this time
 * converting each character to uppercase before
 * printing.
 */

public class ByteArrayInputStreamDemo
{

    public static void main(String[] args) throws IOException
    {
        String str = "ram";
        byte byteArray[] = str.getBytes();
        ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(byteArray);
        for (int i = 0; i < 2; i++)
        {
            int c;
            while ((c = byteArrayInputStream.read()) != -1)
            {
                if (i == 0)
                {
                    System.out.print((char) c);
                }
                else
                {
                    System.out.print(Character.toUpperCase((char) c));
                }
            }
            System.out.println();
            /*
             * A ByteArrayInputStream implements both mark()
             * and reset(). However, if mark() has not been
             * called, then reset() sets the stream pointer
             * to the start of the stream, which in this
             * case is the start of the byte array passed to
             * the constructor.
             * 
             * Resets the buffer to the marked position. The
             * marked position is 0 unless another position
             * was marked or an offset was specified in the
             * constructor.
             */
            byteArrayInputStream.reset();
        }
    }

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

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

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