Skip Read Part File

Skip Read Part File

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


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?

8 thoughts on “Skip Read Part File

          1. M.pavan

            same code sir but am placing the try block before this line “FileInputStream fis = new FileInputStream(“Morals.txt”); ” not at the main() method??

  1. k rakesh kumar

    sir,

    I just had gone through the above program,skipping the bytes from the files,in the above program i find that last 30 bytes are skipped,suppose if we want to skip only 20 characters in the middle of the file,and the remaining part of the file as to be read, what we should do.

    thankyou.

Leave a Reply

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