Java.io.ByteArrayInputStream.skip() Method


Advertisements


Description

The java.io.ByteArrayInputStream.skip(long n) method skips n bytes from the input stream.

Declaration

Following is the declaration for java.io.ByteArrayInputStream.skip(long n) method −

public long skip(long n)

Parameters

n − The count of bytes to be skipped

Return Value

This method returns the actual number of bytes skipped.

Exception

NA

Example

The following example shows the usage of java.io.ByteArrayInputStream.skip(long n) method.

package com.tutorialspoint;

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

public class ByteArrayInputStreamDemo {
   
   public static void main(String[] args) throws IOException {
      byte[] buf = {65, 66, 67, 68, 69};
      ByteArrayInputStream bais = null;
      
      try {
         
         // create new byte array input stream
         bais = new ByteArrayInputStream(buf);
         
         int value = 0;
         
         // read till the end of the stream
         while((value = bais.read())!=-1) {
            
            // skip single byte
            bais.skip(1);
            System.out.println(value);
         }
         
      } catch(Exception e) {

         // if I/O error occurs
         e.printStackTrace();
      } finally {
         if(bais!=null)
            bais.close();
      }   
   }
}

Let us compile and run the above program, this will produce the following result −

65
67
69

java_io_bytearrayinputstream.htm

Advertisements