Skip n bytes while reading the file using FileInputStream
- /*
- Skip n bytes while reading the file using FileInputStream
- This example shows how to skip n bytes while reading the file using skip method of
- Java FileInputStream class.
- */
- import java.io.*;
- public class SkipBytesReadFile {
- public static void main(String[] args) {
- //create file object
- File file = new File("C://FileIO//ReadString.txt");
- try
- {
- //create FileInputStream object
- FileInputStream fin = new FileInputStream(file);
- int ch;
- /*
- * To skip n bytes while reading the file, use
- * int skip(int nBytes) method of Java FileInputStream class.
- *
- * This method skip over n bytes of data from stream. This method returns
- * actual number of bytes that have been skipped.
- */
- //skip first 10 bytes
- fin.skip(10);
- while( (ch = fin.read()) != -1 )
- System.out.print((char) ch);
- }
- catch(FileNotFoundException e)
- {
- System.out.println("File not found" + e);
- }
- catch(IOException ioe)
- {
- System.out.println("Exception while reading the file " + ioe);
- }
- }
- }



