Java - LineNumberInputStream setLineNumber(int lineNumber) method



Description

The Java LineNumberInputStream setLineNumber(int lineNumber) method sets the current line number to the specified value y. However, LineNumberInputStream is deprecated and replaced by LineNumberReader. Despite this, here are two examples demonstrating how setLineNumber(int y) works.

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 - Usage of LineNumberInputStream setLineNumber(int lineNumber) method

The following example shows the usage of Java LineNumberInputStream setLineNumber(int lineNumber) method.

LineNumberInputStreamDemo.java

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("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();      
      }
   }
}

Output(Assuming test.txt contains "ABCDE")

Let us compile and run the above program, this will produce the following result−

Line number: 100

Example - Setting the Line Number Manually

The following example shows the usage of Java LineNumberInputStream setLineNumber(int lineNumber) method. This example manually sets the line number and reads the input.

LineNumberInputStreamDemo.java

package com.tutorialspoint;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.LineNumberInputStream;

public class LineNumberInputStreamDemo {
   public static void main(String[] args) {
      String text = "Hello\nWorld\nJava Programming";
      byte[] data = text.getBytes();

      try {
         ByteArrayInputStream bais = new ByteArrayInputStream(data);
         LineNumberInputStream lnis = new LineNumberInputStream(bais);

         // Set an arbitrary line number
         lnis.setLineNumber(10);
         System.out.println("Initial Line Number: " + lnis.getLineNumber());

         // Read characters to observe line changes
         while (lnis.read() != -1) {
            // Print the current line number
            System.out.println("Current Line Number: " + lnis.getLineNumber());
         }

         lnis.close();
      } catch (IOException e) {
         e.printStackTrace();
      }
   }
}

Output

Let us compile and run the above program, this will produce the following result−

Initial Line Number: 10
Current Line Number: 10
Current Line Number: 10
Current Line Number: 10
Current Line Number: 10
Current Line Number: 10
Current Line Number: 11
Current Line Number: 11
Current Line Number: 11
Current Line Number: 11
Current Line Number: 11
Current Line Number: 11
Current Line Number: 12
Current Line Number: 12
Current Line Number: 12
Current Line Number: 12
Current Line Number: 12
Current Line Number: 12
Current Line Number: 12
Current Line Number: 12
Current Line Number: 12
Current Line Number: 12
Current Line Number: 12
Current Line Number: 12
Current Line Number: 12
Current Line Number: 12
Current Line Number: 12
Current Line Number: 12
Current Line Number: 12

Explanation

  • A string with multiple lines is converted to a byte array.

  • LineNumberInputStream reads from ByteArrayInputStream.

  • setLineNumber(10) sets the initial line number to 10.

  • As the stream reads characters, it prints the current line number.

Example - Resetting the Line Number Mid-Stream

The following example shows the usage of Java LineNumberInputStream setLineNumber(int lineNumber) method. This example shows how to reset the line number after reading some data.

LineNumberInputStreamDemo.java

package com.tutorialspoint;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.LineNumberInputStream;

public class LineNumberInputStreamDemo {
   public static void main(String[] args) {
      String text = "First Line\nSecond Line\nThird Line";
      byte[] data = text.getBytes();

      try {
         ByteArrayInputStream bais = new ByteArrayInputStream(data);
         LineNumberInputStream lnis = new LineNumberInputStream(bais);

         // Read some data
         byte[] buffer = new byte[15];
         lnis.read(buffer, 0, buffer.length);
         System.out.println("Read Data: " + new String(buffer));
         System.out.println("Line Number Before Reset: " + lnis.getLineNumber());

         // Reset line number manually
         lnis.setLineNumber(5);
         System.out.println("Line Number After Reset: " + lnis.getLineNumber());

         lnis.close();
      } catch (IOException e) {
         e.printStackTrace();
      }
   }
}

Output

Let us compile and run the above program, this will produce the following result−

Read Data: First Line
Seco
Line Number Before Reset: 1
Line Number After Reset: 5

Explanation

  • Reads the first 15 bytes from the input stream.

  • Prints the line number before resetting it.

  • Calls setLineNumber(5) to reset the line count.

  • Prints the updated line number.

java_io_linenumberinputstream.htm
Advertisements