Example of skip() method of CheckedInputStream class.
In this section we will discuss about skip method of CheckedInputStream class to skip the byte in a given file .
In this section we will discuss about skip method of CheckedInputStream class to skip the byte in a given file .
Example of skip() method of CheckedInputStream class.
In this tutorial, we will discuss the use of skip() method of CheckedInputStream
class. The CheckedInputStream class creates a input stream which maintains
checksum at reading time and calculate checksum value.The CheckedInputStream class available in
java.util.zip package. The skip() method is used to skip n bytes on associated
input stream.
In the given Example, we
will explain how to use skip() method of CheckedInputStream class.
The CheckedInputStream class creates a input stream which maintains
checksum at reading time and calculate checksum value. The File class
creates an instance of file. The Length() method of File class returns
length of file. The read() method of CheckedOutputStream class
read a bytes from input stream.
About CheckedInputStream API:
The java.util.zip.CheckedInputStream class extends java.util.zip.FilterOutputStream
class. It provid following method:
Return Type |
Method |
Description |
long |
skip() |
The skip() method skip given number of bytes of
input stream. |
void |
read() |
The read() method read a byte from input stream. |
Code
import java.io.File;
import java.io.FileInputStream;
import java.util.zip.CRC32;
import java.util.zip.CheckedInputStream;
import java.io.IOException;
class Demo {
public static String fileName = "bharat.txt";
public static long fileSize;
public static void doCheckRead() throws IOException {
try {
CheckedInputStream cis = new CheckedInputStream(
new FileInputStream(fileName), new CRC32());
File file = new File(fileName);
fileSize = file.length();
System.out.print("Content of file before skiping : ");
for (int i = 0; i < fileSize; i++) {
System.out.print((char) cis.read());
}
} catch (IOException e) {
System.out.println("IOException : " + e);
}
}
public static void doCheckSkip() {
try {
CheckedInputStream cis = new CheckedInputStream(
new FileInputStream(fileName), new CRC32());
File file = new File(fileName);
fileSize = file.length();
System.out.print("Content of file after skiping : ");
for (int i = 0; i < fileSize; i++) {
if (i == 2) {
long checkSkip = cis.skip(3);
i = i + 3;
}
System.out.print((char) cis.read());
}
} catch (IOException e) {
System.out.println("IOException : " + e);
}
}
}
public class CheckSkip {
public static void main(String[] args) throws IOException {
Demo.doCheckRead();
Demo.doCheckSkip();
}
}
|
Following is the output if you run the application:
C:\>java CheckSkip
Content of file before skiping : roseindia
Content of file after skiping : rondia |
Download this code