Java FileInputStream skip() Example
FileInputStream class skip() method example. This example shows you how to use skip() method.
Syntax is : public long skip(long n) throws IOException
Method skips over and discards n bytes of data from the input stream. The skip method may, for a variety of reasons, end up skipping over some smaller number of bytes, possibly 0. If n is negative, an IOException is thrown, even though the skip method of the InputStream superclass does nothing in this case. The actual number of bytes skipped is returned.
Here is the code.
/**
* @(#) SkipFileInputStream.java
* A class representing use of method skip() of FileInputStream
class in java.io Package.
* @Version 07-June-2008
* @author Rose India Team
*/
import java.io.*;
public class SkipFileInputStream {
public static void main(String[] args) throws IOException {
File file = new File("Mahendra.txt");
byte[] b = new byte[]{'M','A','H','E','S','I','N','N','D','R','A'};
FileOutputStream objFileOutStream = new FileOutputStream(file);
objFileOutStream.write(b);
System.out.print("Content of file : " );
for(int i = 0;i<b.length; i++){
System.out.print((char)b[i]);
}
// Create object of FileInputStream class.
FileInputStream objFileInStream = new FileInputStream(file);
System.out.print("\nElements read from the file stream : ");
for( int i=0; i<b.length; i++) {
if(i==4){
long skip = objFileInStream.skip(3);
System.out.print("\nAfter skip "+skip+" elements of file "
+" Stream next elements are : ");
i=i+3;
}
System.out.print((char)objFileInStream.read());
}
}
} |
output of the program.
Content of file : MAHESINNDRA
Elements read from the file stream : MAHE
After skip 3 elements of file Stream next elements are : NDRA |
|