Java Code Example for BufferedInputStream: skip() method

skip() method of BufferedInputStream class creates a byte array and then repeatedly reads into it until n bytes have been read or the end of the stream has been reached.

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 0. This may result from any of a number of conditions; reaching end of file before n bytes have been skipped is only one possibility. The actual number of bytes skipped is returned. If n is negative, no bytes are skipped.

/**********************************************************************************
 * Created on Nov, 2004 Copyright(c) https://kodehelp.com All Rights Reserved. 
 **********************************************************************************/
package com.kodehelp.java.io;

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

/**
 * @author https://kodehelp.com
 *
 */
public class BufferedInputStreamSkip {

	/**
	 * @param args
	 * @throws IOException 
	 */
	public static void main(String[] args) throws IOException {
		FileInputStream fileInputStream = new FileInputStream(new File("/test.txt"));
	    BufferedInputStream bufferedInputStream = new BufferedInputStream(fileInputStream);
	    while (bufferedInputStream.available() > 0) {
	      System.out.println((char) bufferedInputStream.read());
	      bufferedInputStream.skip(2);
	    }
	    bufferedInputStream.close();

	}

}

Please Post Your Comments & Reviews

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.