Friday, 29 July 2016

Java Tutorial : Java IO (BufferedInputStream - destArray)


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

myinputfile.txt
Peter is going to India.

BufferedInputStreamDemo.java
import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.IOException;

/*
 * public int read(byte[] b, int off, int len)
 * 
 * Parameters:
 * ----------
 * 
 * b - destination buffer. 
 * off - offset at which to start storing bytes. 
 * len - maximum number of bytes to read.
 */
public class BufferedInputStreamDemo
{

    public static void main(String[] args) throws IOException
    {
        BufferedInputStreamDemo bufferedInputStreamDemo = new BufferedInputStreamDemo();
        bufferedInputStreamDemo.readFile();
    }

    private void readFile() throws IOException
    {
        FileInputStream fileInputStream = null;
        BufferedInputStream bufferedInputStream = null;

        try
        {
            fileInputStream = new FileInputStream("myinputfile.txt");
            bufferedInputStream = new BufferedInputStream(fileInputStream);

            byte[] destBuffer = new byte[10];

            /*
             * Reads bytes from this byte-input stream into
             * the specified byte array, starting at the
             * given offset.
             * 
             * the number of bytes read, or -1 if the end of
             * the stream has been reached.
             */
            int numberOfBytesRead =bufferedInputStream.read(destBuffer, 0, 5);
            System.out.println("numberOfBytesRead = "+numberOfBytesRead);

            for (byte b : destBuffer)
            {
                System.out.print((char) b);
            }

        }

        finally
        {
            if (fileInputStream != null)
            {
                fileInputStream.close();
            }
            if (bufferedInputStream != null)
            {
                bufferedInputStream.close();
            }
        }
    }
}
Output
numberOfBytesRead = 5
Peter
Click the below link to download the code:
https://sites.google.com/site/ramj2eev1/home/javabasics/JavaIODemo_BufferedInputStream_DestBuffer_App.zip?attredirects=0&d=1

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

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