Java FilterInputStream skip() Example

FilterInputStream class skip() method example. This example shows you how to use skip() method.


Syntax is : public long skip(long n) throws IOException
This method skips over and discards n bytes of data from this input stream. This method simply performs in.skip(n).

Here is the code.
/**
 * @(#) SkipFilterInputStream.java
 * A class representing use of method skip() of FilterInputStream 
 class in java.io Package.
 * @Version  09-June-2008
 @author   Rose India Team
 */
import java.io.*;

public class SkipFilterInputStream extends FilterInputStream {

    SkipFilterInputStream(FileInputStream objFin){
    super(objFin);
    }

  public static void main(String[] argsthrows IOException{

        // Declare the buffer and initialize its size:
        byte[] arrByte = new  byte[1024];
    byte[] byteArray = new byte[]{'M','A','H','E','N','D','R','A'};

    //Create a file with a specified name and file extension.
    File file = new File("mahendra.txt");

    // Create a output stream and write specified bytes in file.
        FileOutputStream objFileStream = new FileOutputStream(file);
    objFileStream.write(byteArray);

    // create a file input stream to read content of the file.
      FileInputStream objFin = new FileInputStream(file);
    SkipFilterInputStream obj =new SkipFilterInputStream(objFin);

        System.out.print("\nElements read from the file stream : ");
            forint i=0; i<byteArray.length; i++) {
        if(i==4){
          long skip = obj.skip(3);
          System.out.print("\nAfter skip "+skip+" elements of input "
                +" Stream next elements are : ");
            i=i+3;
        }
            System.out.print((char)obj.read());
            }
    }
}

Output of the program.
 Elements read from the file stream : MAHE
After skip 3 elements of input  Stream next elements are : A

Post Comment
Name:
E-mail:
Contact no :
Comments:
  Refresh Image
Verify Image:
 
 
Your Comment's
 
 
 

HOME | COPYRIGHT | CONTACT US