Home » Java programming language

Java LineNumberInputStream setLineNumber() Method with Example

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

LineNumberInputStream Class setLineNumber() method

  • setLineNumber() method is available in java.io package.
  • setLineNumber() method is used to set the line number of this LineNumberInputStream 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 represents the new line number to set.

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 
// LineNumberInputStream

import java.io.*;

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

  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 setLineNumber() method is to set
    // the given line number of the given byte exists
    // in this stream line_stm

    line_stm.setLineNumber(set_ln);
    // 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.setLineNumber(): " + line_num);

    set_ln = set_ln + 1;
   }

  } 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.setLineNumber(): 1
byte: 65 :line_stm.setLineNumber(): 2
byte: 86 :line_stm.setLineNumber(): 3
byte: 65 :line_stm.setLineNumber(): 4
byte: 87 :line_stm.setLineNumber(): 5
byte: 79 :line_stm.setLineNumber(): 6
byte: 82 :line_stm.setLineNumber(): 7
byte: 76 :line_stm.setLineNumber(): 8
byte: 68 :line_stm.setLineNumber(): 9





Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.





Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates


© https://www.includehelp.com some rights reserved.