Home » Java programming language

Java LineNumberReader setLineNumber() Method with Example

LineNumberReader Class setLineNumber() method: Here, we are going to learn about the setLineNumber() method of LineNumberReader Class with its syntax and example.
Submitted by Preeti Jain, on April 16, 2020

LineNumberReader Class setLineNumber() method

  • setLineNumber() method is available in java.io package.
  • setLineNumber() method is used to set the present line number to the given line number (line_num).
  • setLineNumber() method is a non-static method, it is accessible with the class object only and if we try to access the method with the class name then we will get an error.
  • setLineNumber() method does not throw an exception at the time of set line number.

Syntax:

    public void setLineNumber(int line_num);

Parameter(s):

  • int line_num – represents the newly set current line number.

Return value:

The return type of the method is void, it returns nothing.

Example:

// Java program to demonstrate the example 
// of void setLineNumber(int line_num) method 
// of LineNumberReader

import java.io.*;

public class SetLineNumberOfLNR {
 public static void main(String[] args) throws Exception {
  FileReader fr_stm = null;
  LineNumberReader line_r = null;
  int val = 0;

  try {
   // Instantiates FileReader and LineNumberReader
   fr_stm = new FileReader("D:\\includehelp.txt");
   line_r = new LineNumberReader(fr_stm);

   // By using setLineNumber() method is to
   // set the current line number in this
   // stream line_r
   line_r.setLineNumber(1);

   // By using getLineNumber() method is to
   // return the current line number in this
   // stream line_r
   int line_num = line_r.getLineNumber();
   System.out.println("line_r.setLineNumber(1): " + line_num);
  } catch (Exception ex) {
   System.out.println(ex.toString());
  } finally {
   // with the help of this block is to
   // free all necessary resources linked
   // with the stream
   if (fr_stm != null) {
    fr_stm.close();
    if (line_r != null) {
     line_r.close();
    }
   }
  }
 }
}

Output

line_r.setLineNumber(1): 1



Comments and Discussions!

Load comments ↻






Copyright © 2024 www.includehelp.com. All rights reserved.