Java.io.LineNumberInputStream.skip() Method


Advertisements


Description

The java.io.LineNumberInputStream.skip(long n) skips over and discards n bytes of data from this input stream.

Declaration

Following is the declaration for java.io.LineNumberInputStream.skip(long n) method −

public long skip(long n)

Parameters

n − The number of bytes to be skipped.

Return Value

The method returns the actual number of bytes to be skipped.

Exception

IOException − If an I/O error occurs.

Example

The following example shows the usage of java.io.LineNumberInputStream.skip(long n) method.

package com.tutorialspoint;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.LineNumberInputStream;

public class LineNumberInputStreamDemo {

   public static void main(String[] args) throws IOException {
      LineNumberInputStream lnis = null;
      FileInputStream fis = null;
      int i;
      char c;
      
      try {
      
         // create new input stream
         fis = new FileInputStream("C:/test.txt");
         lnis = new LineNumberInputStream(fis);
         
         // read till the end of the file
         while((i = lnis.read())!=-1) {
         
            // converts int to char
            c = (char)i;
            
            // prints
            System.out.println(c);
            
            // skips one byte
            lnis.skip(1);
         }
         
      } catch(Exception e) {
         
         // if any error occurs
         e.printStackTrace();
      } finally {
         
         // closes the stream and releases any system resources
         if(fis!=null)
            fis.close();
         if(lnis!=null)
            lnis.close();      
      }
   }
}

Assuming we have a text file c:/test.txt, which has the following content. This file will be used as an input for our example program −

ABCDE

Let us compile and run the above program, this will produce the following result −

A
C
E

java_io_linenumberinputstream.htm

Advertisements