Java.io.LineNumberReader.skip() Method

Advertisements


Description

The java.io.LineNumberReader.skip(long n) method skip n characters.

Declaration

Following is the declaration for java.io.LineNumberReader.skip(long n) method:

public long skip(long n)

Parameters

  • n -- The number of characters to skip.

Return Value

The number of characters actually skipped

Exception

  • IOException -- if an I/O error occurs

  • IllegalArgumentException -- if n is negative

Example

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

package com.tutorialspoint;

import java.io.FileReader;
import java.io.IOException;
import java.io.LineNumberReader;

public class LineNumberReaderDemo {
   public static void main(String[] args) throws IOException {
      
      FileReader fr = null;
      LineNumberReader lnr = null;
      int i;
      char c;
      
      try{
         // create new reader
         fr = new FileReader("C:/test.txt");
         lnr = new LineNumberReader(fr);
   
         // read till the end of the stream
         while((i=lnr.read())!=-1)
         {
            // skip one byte
            lnr.skip(1);
            
            // converts integer to char 
            c=(char)i;
            
            // prints char
            System.out.print(c);
         }
      }catch(Exception e){
         
         // if any error occurs
         e.printStackTrace();
      }finally{
         
         // closes the stream and releases system resources
         if(fr!=null)
            fr.close();
         if(lnr!=null)
            lnr.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:

ACE



Advertisements
Advertisements