Java.io.LineNumberInputStream.setLineNumber() Method

Advertisements


Description

The java.io.LineNumberInputStream.setLineNumber(int lineNumber) methods sets the line number to the provided argument.

Declaration

Following is the declaration for java.io.LineNumberInputStream.setLineNumber(int lineNumber) method:

public void setLineNumber(int lineNumber)

Parameters

  • lineNumber -- the new line number.

Return Value

The method does not return any value.

Exception

  • NA

Example

The following example shows the usage of java.io.LineNumberInputStream.setLineNumber(int lineNumber) 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;
      
      try{
         // create new input streams
         fis = new FileInputStream("C:/test.txt");
         lnis = new LineNumberInputStream(fis);
         
         // set the line number
         lnis.setLineNumber(100);
         
         // get the current line number
         i=lnis.getLineNumber();
         
         // prints
         System.out.print("Line number: "+i);
      }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:

Line number: 100



Advertisements
Advertisements