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
  • Java Tutorial : Java IO (ByteArrayOutputStream)- Playlist

    Java Tutorial : Java IO (ByteArrayOutputStream read input from a user)


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

    ByteArrayOutputStreamDemo.java
    import java.io.ByteArrayInputStream;
    import java.io.ByteArrayOutputStream;
    import java.io.IOException;
    
    public class ByteArrayOutputStreamDemo
    {
    
        public static void main(String[] args) throws IOException
        {
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
    
            System.out.println("Enter the chars:");
            while (byteArrayOutputStream.size() != 4)
            {
                // Gets the inputs from the user
                byteArrayOutputStream.write(System.in.read());
            }
    
            byte[] byteArray = byteArrayOutputStream.toByteArray();
            
            System.out.println("\nPrint the content");
            for (int x = 0; x < byteArray.length; x++)
            {
                // printing the characters
                System.out.print((char) byteArray[x] + "   ");
            }
            System.out.println("   \n");
    
            int c;
    
            ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(byteArray);
    
            System.out.println("Converting characters to Upper case ");
            for (int y = 0; y < 1; y++)
            {
                while ((c = byteArrayInputStream.read()) != -1)
                {
                    System.out.print(Character.toUpperCase((char) c)
                            + "   ");
                }
            }
        }
    
    }
    
    Output
    Enter the chars:
    abcd
    
    Print the content
    a   b   c   d      
    
    Converting characters to Upper case 
    A   B   C   D   
    
    Click the below link to download the code:
    https://sites.google.com/site/ramj2eev1/home/javabasics/JavaIODemo_ByteArrayOS_ByteArrayIS_App.zip?attredirects=0&d=1

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

    Bitbucket Link:
    https://bitbucket.org/ramram43210/java/src/d261aab7f0b47dd247e532c02763fe1d7053f5d3/BasicJava/JavaIODemo_ByteArrayOS_ByteArrayIS_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
  • Java Tutorial : Java IO (ByteArrayOutputStream write to multiplefiles)


    Click here to watch in Youtube :
    https://www.youtube.com/watch?v=68zVPj-H_B0&list=UUhwKlOVR041tngjerWxVccw

    Click the below Image to Enlarge
    Java Tutorial : Java IO (ByteArrayOutputStream write to multiplefiles) 
    ByteArrayOutputStreamDemo.java
    import java.io.ByteArrayOutputStream;
    import java.io.FileOutputStream;
    import java.io.IOException;
    
    public class ByteArrayOutputStreamDemo
    {
    
        public static void main(String[] args) throws IOException
        {
            FileOutputStream fileOutputStream1 = null;
            FileOutputStream fileOutputStream2 = null;
            ByteArrayOutputStream byteArrayOutputStream = null;
            try
            {
                fileOutputStream1 = new FileOutputStream("myfile1.txt");
                fileOutputStream2 = new FileOutputStream("myfile2.txt");
                byteArrayOutputStream = new ByteArrayOutputStream();
    
                String str = "Welcome to India";
                byte[] byteArray = str.getBytes();
                byteArrayOutputStream.write(byteArray);
    
                /*
                 * Writes the complete contents of this byte
                 * array output stream to the specified output
                 * stream argument
                 */
                byteArrayOutputStream.writeTo(fileOutputStream1);
                byteArrayOutputStream.writeTo(fileOutputStream2);
    
                byteArrayOutputStream.flush();
                System.out.println("successfully written to two files...");
            }
            finally
            {
                if (fileOutputStream1 != null)
                {
                    fileOutputStream1.close();
                }
                if (fileOutputStream2 != null)
                {
                    fileOutputStream2.close();
                }
                if (byteArrayOutputStream != null)
                {
                    byteArrayOutputStream.close();
                }
            }
        }
    }
    
    Output
    successfully written to two files...
    
    myfile1.txt
    Welcome to India
    
    myfile2.txt
    Welcome to India
    
    Click the below link to download the code:
    https://sites.google.com/site/ramj2eev1/home/javabasics/JavaIODemo_ByteArrayOS_Writeto_multipleFiles_App.zip?attredirects=0&d=1

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

    Bitbucket Link:
    https://bitbucket.org/ramram43210/java/src/d261aab7f0b47dd247e532c02763fe1d7053f5d3/BasicJava/JavaIODemo_ByteArrayOS_Writeto_multipleFiles_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
  • Java Tutorial : Java IO (ByteArrayOutputStream)


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

    Click the below Image to Enlarge
    Java Tutorial : Java IO (ByteArrayOutputStream)
    Java Tutorial : Java IO (ByteArrayOutputStream)
    Java Tutorial : Java IO (ByteArrayOutputStream)
    ByteArrayOutputStreamDemo.java
    import java.io.ByteArrayOutputStream;
    import java.io.IOException;
    
    public class ByteArrayOutputStreamDemo
    {
    
        public static void main(String[] args) throws IOException
        {
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
    
            String str = "Welcome to India.";
            byte[] byteArray = str.getBytes();
            byteArrayOutputStream.write(byteArray);
    
            System.out.println("Buffer as a string");
            /*
             * Returns String decoded from the buffer's
             * contents.
             */
            String content = byteArrayOutputStream.toString();
            System.out.println(content);
    
            System.out.println("----------------------------------");
            System.out.println("Into array");
    
            /*
             * Returns the current contents of this output
             * stream, as a byte array
             */
            byte b[] = byteArrayOutputStream.toByteArray();
    
            for (int i = 0; i < b.length; i++)
            {
                System.out.print((char) b[i]);
            }
        }
    
    }
    
    Output
    Buffer as a string
    Welcome to India.
    ----------------------------------
    Into array
    Welcome to India.

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

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

    Bitbucket Link:
    https://bitbucket.org/ramram43210/java/src/d261aab7f0b47dd247e532c02763fe1d7053f5d3/BasicJava/JavaIODemo_ByteArrayOutputStream_Intro_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
  • Java Tutorial : Java IO (ByteArrayInputStream available method)


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

    ByteArrayInputStreamDemo.java
    import java.io.ByteArrayInputStream;
    import java.io.IOException;
    
    public class ByteArrayInputStreamDemo
    {
    
        public static void main(String[] args) throws IOException
        {
            String str = "Hello World!";
            byte[] byteArray = str.getBytes();
            ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(byteArray);
    
            /*
             * Returns: 
             * ------- 
             * 
             * The number of remaining bytes
             * that can be read (or skipped over) from this
             * input stream without blocking.
             * 
             * The value returned is count - pos, which is the
             * number of bytes remaining to be read from the
             * input buffer.
             */
            while (byteArrayInputStream.available() != 0)
            {
                System.out.print(new Character((char) byteArrayInputStream.read()));
            }
        }
    
    }
    
    Output
    Hello World!
    
    Click the below link to download the code:
    https://sites.google.com/site/ramj2eev1/home/javabasics/JavaIODemo_ByteArrayIS_available_App.zip?attredirects=0&d=1

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

    Bitbucket Link:
    https://bitbucket.org/ramram43210/java/src/d261aab7f0b47dd247e532c02763fe1d7053f5d3/BasicJava/JavaIODemo_ByteArrayIS_available_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
  • 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
  • Java Tutorial : Java IO (ByteArrayInputStream Constructors)


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

    ByteArrayInputStreamDemo.java
    import java.io.ByteArrayInputStream;
    import java.io.IOException;
    
    public class ByteArrayInputStreamDemo
    {
    
        public static void main(String[] args) throws IOException
        {
            String str = "abcdefghijklmnopqrstuvwxyz";
            byte[] byteArray = str.getBytes();
    
            ByteArrayInputStream byteArrayInputStream1 = new ByteArrayInputStream(byteArray);
    
            /*
             * The byteArrayInputStream1 contains all letters.
             */
            displayContent(byteArrayInputStream1);
    
            ByteArrayInputStream byteArrayInputStream2 = new ByteArrayInputStream(byteArray, 0, 3);
    
            /*
             * The byteArrayInputStream2 contains only the first
             * three letters.
             */
            displayContent(byteArrayInputStream2);
    
        }
    
        private static void displayContent(ByteArrayInputStream byteArrayInputStream)
        {
            int ch;
            while ((ch = byteArrayInputStream.read()) != -1)
            {
                System.out.print((char) ch);
            }
            System.out.println();
        }
    }
    
    Output
    abcdefghijklmnopqrstuvwxyz
    abc
    
    
    Click the below link to download the code:
    https://sites.google.com/site/ramj2eev1/home/javabasics/JavaIODemo_ByteArrayIS_Cons_App.zip?attredirects=0&d=1

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

    Bitbucket Link:
    https://bitbucket.org/ramram43210/java/src/d261aab7f0b47dd247e532c02763fe1d7053f5d3/BasicJava/JavaIODemo_ByteArrayIS_Cons_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
  • Friday, 26 August 2016

    Java Tutorial : Java IO (ByteArrayInputStream)- Playlist

    Java Tutorial : Java IO (ByteArrayInputStream Capitalize)


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

    ByteArrayInputStreamDemo.java
    import java.io.ByteArrayInputStream;
    import java.io.IOException;
    import java.util.Scanner;
    
    /*
     * Another simple usage of ByteArrayInputStream would be
     * a way of capitalizing the input from the user.
     */
    
    public class ByteArrayInputStreamDemo
    {
    
        public static void main(String[] args) throws IOException
        {
            Scanner scanner = new Scanner(System.in);
    
            System.out.print("Enter the name : ");
            String name = scanner.nextLine();
    
            scanner.close();
    
            ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(name.getBytes());
    
            StringBuilder sb = new StringBuilder();
    
            int ch;
            while ((ch = byteArrayInputStream.read()) != -1)
            {
                sb.append(Character.toUpperCase((char) ch));
            }
            System.out.println("Your capitalized name: " + sb.toString());
    
        }
    }
    
    Output
    Enter the name : peter
    Your capitalized name: PETER
    
    
    Click the below link to download the code:
    https://sites.google.com/site/ramj2eev1/home/javabasics/JavaIODemo_ByteArrayIS_Capitilization_App.zip?attredirects=0&d=1

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

    Bitbucket Link:
    https://bitbucket.org/ramram43210/java/src/94d38fdd49b2d33c14c088526d40f902c21a1e0d/BasicJava/JavaIODemo_ByteArrayIS_Capitilization_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
  • Java Tutorial : Java IO (ByteArrayInputStream Copy to destArray)


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

    ByteArrayInputStreamDemo.java
    import java.io.ByteArrayInputStream;
    import java.io.IOException;
    
    /*
     * public int read(byte[] b, int off, int len)
     * 
     * b - the buffer into which the data is read.
     * 
     * off - the start offset in the destination
     * array b 
     * 
     * len - the maximum number of bytes
     * read.
     * 
     * Reads up to len bytes of data into an array
     * of bytes from this input stream.
     */
    public class ByteArrayInputStreamDemo
    {
    
        public static void main(String[] args) throws IOException
        {
            ByteArrayInputStream byteArrayInputStream = null;
    
            try
            {
                byte[] srcBuffer = new byte[10];
    
                int j = 11;
                for (int i = 0; i < srcBuffer.length; i++)
                {
                    srcBuffer[i] = (byte) j++;
                }
    
                System.out.println("All elements form srcBuffer:");
                for (int i = 0; i < srcBuffer.length; i++)
                {
                    System.out.print(srcBuffer[i] + " ");
                }
    
                byteArrayInputStream = new ByteArrayInputStream(srcBuffer);
    
                byte[] destBuffer = new byte[6];
    
                /*
                 * We put 4 first elements of the
                 * ByteArrayInputStream instance
                 * 'byteArrayInputStream' to the destBuffer
                 * array, starting at the position with index 2.
                 * This is why the two first indexes will be
                 * '0'.
                 */
                byteArrayInputStream.read(destBuffer, 2, 4);
    
                System.out.println("\n\nAll elements form destBuffer:");
                for (int i = 0; i < destBuffer.length; i++)
                {
                    System.out.print(destBuffer[i] + " ");
                }
    
            }
            finally
            {
                if (byteArrayInputStream != null)
                {
                    /*
                     * Closing a ByteArrayInputStream has no
                     * effect. The methods in this class can be
                     * called after the stream has been closed
                     * without generating an IOException.
                     */
                    byteArrayInputStream.close();
                }
            }
    
        }
    }
    
    Output
    All elements form srcBuffer:
    11 12 13 14 15 16 17 18 19 20 
    
    All elements form destBuffer:
    0 0 11 12 13 14 
    
    Click the below link to download the code:
    https://sites.google.com/site/ramj2eev1/home/javabasics/JavaIODemo_ByteArrayIS_Copy_to_DestArray_App.zip?attredirects=0&d=1

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

    Bitbucket Link:
    https://bitbucket.org/ramram43210/java/src/94d38fdd49b2d33c14c088526d40f902c21a1e0d/BasicJava/JavaIODemo_ByteArrayIS_Copy_to_DestArray_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
  • Java Tutorial : Java IO (ByteArrayInputStream Read ByteArray numbers)


    Click here to watch in Youtube :
    https://www.youtube.com/watch?v=f-ShohRXo-s&list=UUhwKlOVR041tngjerWxVccw

    ByteArrayInputStreamDemo.java
    import java.io.ByteArrayInputStream;
    import java.io.IOException;
    
    public class ByteArrayInputStreamDemo
    {
    
        public static void main(String[] args) throws IOException
        {
            ByteArrayInputStream byteArrayInputStream = null;
    
            try
            {
                byte[] buffer = new byte[5];
    
                for (int i = 0; i < buffer.length; i++)
                {
                    buffer[i] = (byte) i;
                }
    
                byteArrayInputStream = new ByteArrayInputStream(buffer);
    
                System.out.println("All the elements in the buffer:");
    
                int num;
                /*
                 * Read every number inside the buffer using the
                 * read() method, which returns -1 if the end of
                 * the buffer is reached.
                 */
                while ((num = byteArrayInputStream.read()) != -1)
                {
                    System.out.print(num + " ");
                }
            }
            finally
            {
                if (byteArrayInputStream != null)
                {
                    /*
                     * Closing a ByteArrayInputStream has no
                     * effect. The methods in this class can be
                     * called after the stream has been closed
                     * without generating an IOException.
                     */
                    byteArrayInputStream.close();
                }
            }
    
        }
    }
    
    Output
    All the elements in the buffer:
    0 1 2 3 4 
    
    Click the below link to download the code:
    https://sites.google.com/site/ramj2eev1/home/javabasics/JavaIODemo_ByteArrayIS_Read_ByteArray_Num_App.zip?attredirects=0&d=1

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

    Bitbucket Link:
    https://bitbucket.org/ramram43210/java/src/94d38fdd49b2d33c14c088526d40f902c21a1e0d/BasicJava/JavaIODemo_ByteArrayIS_Read_ByteArray_Num_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
  • Java Tutorial : Java IO (ByteArrayInputStream)


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

    Click the below Image to Enlarge
    Java Tutorial : Java IO (ByteArrayInputStream) 
    Java Tutorial : Java IO (ByteArrayInputStream) 
    Java Tutorial : Java IO (ByteArrayInputStream) 
    ByteArrayInputStreamDemo.java
    import java.io.ByteArrayInputStream;
    import java.io.IOException;
    
    public class ByteArrayInputStreamDemo
    {
    
        public static void main(String[] args) throws IOException
        {
            ByteArrayInputStream byteArrayInputStream = null;
    
            try
            {
                String str = "Peter is going to India.";
                byte[] byteArray = str.getBytes();
                
                byteArrayInputStream = new ByteArrayInputStream(byteArray);
                int ch;
                while ((ch = byteArrayInputStream.read()) != -1)
                {
                    System.out.print((char) ch);
                }
            }
            finally
            {
                if (byteArrayInputStream != null)
                {
                    /*
                     * Closing a ByteArrayInputStream has no
                     * effect. The methods in this class can be
                     * called after the stream has been closed
                     * without generating an IOException.
                     */
                    byteArrayInputStream.close();
                }
            }
    
        }
    }
    Output
    Peter is going to India.
    Refer:
    Click the below link to download the code:
    https://sites.google.com/site/ramj2eev1/home/javabasics/JavaIODemo_ByteArrayInputStream_Intro_App.zip?attredirects=0&d=1

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

    Bitbucket Link:
    https://bitbucket.org/ramram43210/java/src/94d38fdd49b2d33c14c088526d40f902c21a1e0d/BasicJava/JavaIODemo_ByteArrayInputStream_Intro_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
  • Tuesday, 23 August 2016

    Java Tutorial : Java IO (PrintWriter Formatting)


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

    Click the below Image to Enlarge
    Java Tutorial : Java IO (PrintWriter Formatting) 
    FormatDemo1.java
    import java.io.IOException;
    
    public class FormatDemo1
    {
    
        /*
         * The i and r variables are formatted twice: the first
         * time using code in an overload of print, the second
         * time by conversion code automatically generated by
         * the Java compiler, which also utilizes toString. You
         * can format any value this way, but you don't have
         * much control over the results.
         */
        public static void main(String[] args) throws IOException
        {
            int i = 5;
            double r = Math.sqrt(i);
    
            /*
             * Invoking print or println outputs a single value
             * after converting the value using the appropriate
             * toString method
             */
            System.out.print("The square root of ");
            System.out.print(i);
            System.out.print(" is ");
            System.out.print(r);
            System.out.println(".");
    
            i = 10;
            r = Math.sqrt(i);
            System.out.println("The square root of " + i + " is " + r + ".");
    
        }
    }
    
    Output
    The square root of 5 is 2.23606797749979.
    The square root of 10 is 3.1622776601683795.
    
    FormatDemo2.java
    import java.io.IOException;
    
    public class FormatDemo2
    {
    
        public static void main(String[] args) throws IOException
        {
            int i = 20;
            double r = Math.sqrt(i);
            System.out.println("r = "+r);
    
            /*
             * All format specifiers begin with a % and end with
             * a 1- or 2-character conversion that specifies the
             * kind of formatted output being generated. The
             * three conversions used here are:
             * 
             * d - formats an integer value as a decimal value.
             * 
             * f - formats a floating point value as a decimal
             * value.
             * 
             * n - outputs a platform-specific line terminator.
             */
    
            System.out.format("The square root of %d is %f.%n", i, r);
        }
    }
    
    Output
    r = 4.47213595499958
    The square root of 20 is 4.472136.
    
    Click the below link to download the code:
    https://sites.google.com/site/ramj2eev1/home/javabasics/JavaIODemo_PrintWriter_Format_App.zip?attredirects=0&d=1

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

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