Home » Java programming language

Java LineNumberInputStream getLineNumber() Method with Example

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

LineNumberInputStream Class getLineNumber() method

  • getLineNumber() method is available in java.io package.
  • getLineNumber() method is used to return the present line number in this LineNumberInputStream.
  • getLineNumber() 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.
  • getLineNumber() method does not throw an exception at the time of returning line number.

Syntax:

    public int getLineNumber();

Parameter(s):

  • It does not accept any parameter.

Return value:

The return type of the method is int, it returns the current line number in this LineNumberInputStream.

Example:

// Java program to demonstrate the example 
// of int getLineNumber() method of 
// LineNumberInputStream

import java.io.*;

public class GetLineNumberOfLNIS {
 public static void main(String[] args) throws Exception {
  FileInputStream fis_stm = null;
  LineNumberInputStream line_stm = null;
  int val = 0;

  try {
   // Instantiates FileInputStream
   fis_stm = new FileInputStream("D:\\includehelp.txt");
   line_stm = new LineNumberInputStream(fis_stm);

   // Loop to read until available
   // bytes left
   while ((val = line_stm.read()) != -1) {

    // Display corresponding byte value
    byte b = (byte) val;

    // Display value of b
    System.out.print("byte: " + b + " :");

    // By using getLineNumber() method is to return
    // the current line number of the given byte exists
    // in this stream line_stm
    int line_num = line_stm.getLineNumber();
    System.out.println("line_stm.getLineNumber(): " + 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 (fis_stm != null) {
    fis_stm.close();
    if (line_stm != null) {
     line_stm.close();
    }
   }
  }
 }
}

Output

byte: 74 :line_stm.getLineNumber(): 0
byte: 65 :line_stm.getLineNumber(): 0
byte: 86 :line_stm.getLineNumber(): 0
byte: 65 :line_stm.getLineNumber(): 0
byte: 10 :line_stm.getLineNumber(): 1
byte: 87 :line_stm.getLineNumber(): 1
byte: 79 :line_stm.getLineNumber(): 1
byte: 82 :line_stm.getLineNumber(): 1
byte: 76 :line_stm.getLineNumber(): 1
byte: 68 :line_stm.getLineNumber(): 1


Comments and Discussions!

Load comments ↻





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