Skip Read Part File

April 24, 2011 | File Copying

Skip Read Part File

Summary: In this tutorial "Skip Read Part File", you learn to skip a few bytes in a file and read the remaining bytes.

In all the previous examples, the whole file is read from start to end. Java also permits to skip a few bytes in the file and read from there to the end (without reading from the beginning). Here, we use skip() method to place the file pointer at the new position from where we would like to read.

About skip() method

The skip(long) method of InputStream (inherited by FileInputStream) places the file pointer at the specified byte passed as parameter. The operating system maintains a file pointer which moves along the file while reading and writing. The file pointer, by default, is positioned at 0 bytes, the starting position of the file. The skip() method is capable to place the file pointer at any position in the file. The skip() method reads from relative position and can take negative values also. For example, skip(300) and again skip(100) is called, the position is equivalent to calling skip(400) and if skip(-100) is called, it is equivalent to skip(200). There exists one more class in java.io package, RandomAccessFile, that skips always from absolute position with its seek() method.

To illustrate the skip() method, a file, Morals.txt is chosen with some contents. Few bytes are skipped out and the remaining bytes are read.

File Name: Morals.txt

Hard work, sincerity and promptness to your job pays always, if not immediately.

File Name: SkippingAndReading.java

import java.io.*;
public class SkippingAndReading
{
  public static void main(String args[]) throws IOException
  {

    FileInputStream fis = new FileInputStream("Morals.txt"); 
    System.out.println("File size using available() method:" + fis.available());           // prints 80

    fis.skip(30);

    System.out.println("No. of bytes reaming to read after skipping: " + fis.available()); // prints 50 (80 – 30)

    System.out.println("\n\tFile contents from skipping position");
    int temp;
    while(( temp = fis.read()) != -1)
    {
      System.out.print((char) temp);
    }
    fis.close();
                                                                                           // another way of finding the file size
    File f1 = new File("Morals.txt");
    System.out.println("\n\nFile Size using length() method: " + f1.length());             // prints 80
  }
}

Skip Read Part File Output screen of SkippingAndReading.java of Skip Read Part File

fis.available();

The available() method of InputStream, inherited by FileInputStream, job is to return the number of bytes remaining in the file to read yet. But here, as a special case, it returns the file size of Morals.txt. We know, by default, the file pointer is positioned at 0 bytes, the starting poision in the file when the file is opened. The available() method finds the whole file to read (because the file pointer is at 0 positon). The file contents to be read are nothing but the whole file. The same available() method when called after skipping, gives the remaining file length to read.

fis.skip(30);

The above statement skips 30 bytes from the starting position of the file and places the file pointer. Now the read() method reads from the new position.

Do you know there is one more method seek() that does the same job of skip(). Then, what is the difference?