Java PushbackInputStream skip() Example

PushbackInputStream 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. The skip method may, for a variety of reasons, end up skipping over some smaller number of bytes, possibly zero. If n is negative, no bytes are skipped.

Here is the code.

/**
 * @(#) SkipPushbackInputStream.java
 * A class representing use of method skip() of PushbackInputStream 
class in java.io Package.
 * @Version  10-June-2008
 @author   Rose India Team
 */
import java.io.*;

public class SkipPushbackInputStream {

    public static void main(String[] argsthrows IOException {

        // Declare the byte array.
        byte[] byteArray = new byte[]{'M''A''H''E''N''D''R''A'};
        File file = new File("Mahendra.txt");

        // Create object of FileOutputStream class for specified file.
        FileOutputStream fOutStream = new FileOutputStream(file);
        fOutStream.write(byteArray);

        // Create object of PushbackInputStream class for specified stream.
        InputStream inStream = new FileInputStream(file);
        PushbackInputStream pushInStream = new PushbackInputStream(inStream);

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

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

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

HOME | COPYRIGHT | CONTACT US