Java.io - SPLessons

Java.io LineNumberInputStream

Home > Lesson > Chapter 24
SPLessons 5 Steps, 3 Clicks
5 Steps - 3 Clicks

Java.io LineNumberInputStream

Java.io LineNumberInputStream

shape Introduction

The Java.io LineNumberInputStream is the class used to keep track of present line numbers of the byte data. The line number input stream is subclass to reader. A line represents the sequence of characters which ends with '/r' and for a new line character '/n'.

Class Declaration

shape Declaration

Java.io LineNumberInputStream class is declared below: Public class LineNumberInputStream extends Reader

shape Fields

Java.io LineNumberInputStream class fields are:
  • Protected InputStream in : The field represents the current stream have to filter.

Class Constructors

shape Table

Constructor Description
LineNumberInputStream(inputStream in) The function of this constructor is to create the LineNumberInputStream instance and used to read from input stream.

Class Methods

shape Table

Method Description
Long skip (long m) The function of this method is to skip the bytes in this stream of ‘ m ‘ bytes.
int setLineNumber(int LineNumber) The function of this method is to set line number to the represented argument.
int read(byte[] b, int off, int n) By using this method it reads the data up to ‘n’ bytes from the piped input stream into array of  the bytes.
int read() The function of this method is to read one byte of data from this stream.

Inherited Methods

shape Description

From the following classes, methods are inherited to the line number input stream class.
  • Java.io.Object
  • Java.io.FilterInputStream

shape Examples

Usage of Long skip (long m) method. [c]import java.io.FileInputStream; import java.io.IOException; import java.io.LineNumberInputStream; public class LineNumberInputStream { public static void main(String[] args) throws IOException { LineNumberInputStream lnis = null; FileInputStream fis =null; int i; char c; try{ // creating a new input stream fis = new FileInputStream("D:/test.txt"); lnis = new LineNumberInputStream(fis); // To read till the end of the file while((i=lnis.read())!=-1) { // converts integer to character c=(char)i; // To print System.out.println(c); // skips one byte lnis.skip(1); } }catch(Exception e){ // If any error occurs e.printStackTrace(); }finally{ // It closes the stream and releases any system resources if(fis!=null) fis.close(); if(lnis!=null) lnis.close(); } } }[/c] Output D:/test.txt will have the following content. [c]S L S O S [/c] Usage of the int setLineNumber(int LineNumber) method. [c]import java.io.FileInputStream; import java.io.IOException; import java.io.LineNumberInputStream; public class LineNumberInputStream { public static void main(String[] args) throws IOException { LineNumberInputStream lnis = null; FileInputStream fis =null; int i; try{ // creating a new input streams fis = new FileInputStream("D:/test.txt"); lnis = new LineNumberInputStream(fis); // To set the line number lnis.setLineNumber(300); // To get the current line number i=lnis.getLineNumber(); // To print System.out.print("Line number: "+i); }catch(Exception e){ // if any error occurs e.printStackTrace(); }finally{ // It closes the stream and releases any system resources if(fis!=null) fis.close(); if(lnis!=null) lnis.close(); } } }[/c] Output Following is the result will be displayed. [c]300[/c]

Summary

shape Key Points

  • The Java.io LineNumberInputStream is the class used to keep track of present line numbers of the byte data.
  • The line number input stream is subclass to reader.