Java - LineNumberInputStream skip(long n) method



Description

The Java LineNumberInputStream skip(long n) method is used to skip over l bytes in the input stream. It effectively moves the reading position forward by l byte.

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 - Usage of LineNumberInputStream skip(long n) method

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

LineNumberInputStreamDemo.java

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("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();      
      }
   }
}

Output(Assuming test.txt contains "ABCDE")

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

A
C
E

Example - Skipping a Few Bytes

The following example shows the usage of Java LineNumberInputStream skip(long n) method. This example demonstrates skipping 5 bytes in the input stream.

LineNumberInputStreamDemo.java

package com.tutorialspoint;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.LineNumberInputStream;

public class LineNumberInputStreamDemo {
   public static void main(String[] args) {
      String text = "Hello World\nJava Programming";
      byte[] data = text.getBytes();

      try {
         ByteArrayInputStream bais = new ByteArrayInputStream(data);
         LineNumberInputStream lnis = new LineNumberInputStream(bais);

         System.out.println("Reading first character: " + (char) lnis.read());

         lnis.skip(5); // Skip 5 bytes
         System.out.println("After skipping 5 bytes, next character: " + (char) lnis.read());

         System.out.println("Current Line Number: " + lnis.getLineNumber());

         lnis.close();
      } catch (IOException e) {
         e.printStackTrace();
      }
   }
}

Output

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

Reading first character: H
After skipping 5 bytes, next character: W
Current Line Number: 0

Explanation

  • Reads the first character (H).

  • Skips 5 bytes (ello ), moving past "ello ".

  • Reads the next character (W from "World").

  • The line number remains 0 since no newline (\n) was encountered.

Example - Skipping Over Newlines

The following example shows the usage of Java LineNumberInputStream skip(long n) method. This example demonstrates how skip() behaves when skipping past newline characters (\n).

LineNumberInputStreamDemo.java

package com.tutorialspoint;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.LineNumberInputStream;

public class LineNumberInputStreamDemo {
   public static void main(String[] args) {
      String text = "First Line\nSecond Line\nThird Line";
      byte[] data = text.getBytes();

      try {
         ByteArrayInputStream bais = new ByteArrayInputStream(data);
         LineNumberInputStream lnis = new LineNumberInputStream(bais);

         System.out.println("Initial Line Number: " + lnis.getLineNumber());

         lnis.skip(12); // Skip "First Line\n"
         System.out.println("After skipping 12 bytes, next character: " + (char) lnis.read());
         System.out.println("Line Number After Skip: " + lnis.getLineNumber());

         lnis.close();
      } catch (IOException e) {
         e.printStackTrace();
      }
   }
}

Output

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

Initial Line Number: 0
After skipping 12 bytes, next character: e
Line Number After Skip: 1

Explanation

  • The input contains multiple lines.

  • Skip 12 bytes, which includes "First Line\n".

  • The next character read is S from "Second Line".

  • Line number updates to 1, since a newline (\n) was skipped.

java_io_linenumberinputstream.htm
Advertisements