Home Tutorial Java Corejava Zip Skip method in java.

 
 

Share on Google+Share on Google+
Skip method in java.
Posted on: July 14, 2010 at 12:00 AM
Advertisement
In this tutorial you will see how to use the skip method in java.

Skip method in java.

In this tutorial, we will discuss the use of available() and skip() function of  FileInputStream class. The  FileInputStream class creates a input stream for reading bytes from file. The FileInputStream class is available in java.io package.

With the help of this example, you will see how to calculate the size of  file and use of skip function. First of all, we will create input stream for reading bytes from file with the  FileInputStream class. The availabe() method of  the FileInputStream class return available number of bytes in a file, and the skip(long count) method of the FileInputStream class skip given number of byte from input stream.

About FileInputStream API:

The java.io.FileInputStream class extends java.io.IntputStream class. It provides following methods:

Return Type Method Description 
int available()  The available method Returns the integer number of bytes available in this file input stream.
void close()  The close() method use to close this file input stream and system resources used by this stream.
protected
void
finalize()  The finalize() method ensures that all resources for this file are released..
FileDescriptor getFD() The getFD() method return the object of FileDescriptor that represents the connection to the actual file in the file system.
int read() The read() method reads a single byte from the FileInputStream and returns integer value.
int read( byte b, int off, int len ) The read(...) method reads bytes from the FileInputStream and stores them in buffer of byte array.
long skip( int n) Function skip specified number from input stream.

Code

import java.io.*;
import java.util.*;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

class Demo {
  public static void readFile() {
    try {
      File file = new File("testfile.txt");
      FileInputStream inStream = new FileInputStream(file);
      long totalByte = inStream.available();
      System.out.println("\nSize  of file : " + totalByte);
      System.out.print("Content of file  : ");
      for (int i = 0; i < totalByte; i++) {
        System.out.print((charinStream.read());
      }
      inStream.close();
    catch (Exception ioe) {
      System.out.print(ioe);
    }
  }

  public static void doSkip() {
    try {
      File file = new File("testfile.txt");
      FileInputStream inStream = new FileInputStream(file);
      long totalByte = inStream.available();

      System.out
      .print("\nContent after skipping two characters from the file :");
      for (int i = 0; i < totalByte; i++) {
        if (i == 4) {
          long numskip = inStream.skip(2);
          i = i + 2;
        }
        System.out.print((charinStream.read());
      }
      inStream.close();
    catch (Exception ioe) {
      System.out.print(ioe);
    }
  }
}

class SkipDemo {
  public static void main(String[] args) {
    Demo.readFile();
    Demo.doSkip();
  }
}

Following is the output if you run the application :

C:\>java SkipDemo
Size of file : 11
Content of file : bharatsingh
Content after skipping two characters from the file :bharsingh

Download this code

Advertisement

Related Tags for Skip method in java.:


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: July 14, 2010

Recommend the tutorial

Advertisements Advertisements
 

 

 

DMCA.com