Java.io.LineNumberReader.setLineNumber() Method

Advertisements


Description

The java.io.LineNumberReader.setLineNumber(int lineNumber) method sets the current line number.

Declaration

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

public void setLineNumber(int lineNumber)

Parameters

  • lineNumber -- an int specifying the current line number to be set.

Return Value

The method does not return any value.

Exception

  • NA

Example

The following example shows the usage of java.io.LineNumberReader.setLineNumber(int lineNumber) 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;
      
      try{
         // create new reader
         fr = new FileReader("C:/test.txt");
         lnr = new LineNumberReader(fr);
         
         // set current line number
         lnr.setLineNumber(100);
         System.out.println("setLineNumber(100) is invoked");
         
         // get current line number
         i = lnr.getLineNumber();
         System.out.print("Current line number: "+i);
         
      }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:

setLineNumber(100) is invoked
Current line number: 100



Advertisements
Advertisements