Java.io.LineNumberInputStream Class in Java
Last Updated :
23 Jan, 2023
java.io.LineNumberInputStream class is simply an extension of input stream providing a extra facility to keep the record of current line number.
Line is a sequence of bytes ending with : '\r' i.e. a carriage return character or a newline character : '\n', or a linefeed character following the carriage return character.
Declaration :
public class LineNumberInputStream
extends Reader
Constructors :
LineNumberInputStream(InputStream in) :
Constructs a newline no. stream that reads
it's input from the specified Input Stream.
Methods:

- read() : java.io.LineNumberInputStream.read() reads next byte of data from input stream. It returns int value representing the bytes in the range of '0 - 255'. It returns '-1' to indicate end of Input Stream.
Syntax :
public int read()
Parameters :
-------
Return :
int value representing the bytes in the range of '0 - 255'.
return -1, indicating end of Input Stream.
Exception:
IOException : in case I/O error occurs
Implementation :
Java
// Java program illustrating the working of read() method
import java.io.*;
public class NewClass
{
public static void main(String[] args) throws IOException
{
// LineNumberInputStream & FileInputStream initially null
LineNumberInputStream geekline = null;
FileInputStream geekinput = null;
try{
char c;
int a;
// New InputStream : 'ABC' is created
geekinput = new FileInputStream("ABC.txt");
geekline = new LineNumberInputStream(geekinput);
// read() method returning Bytes of Input Stream as integer
// '-1' indicating to read till end Of Input Stream
while((a = geekline.read()) != -1)
{
// Since read() method returns Integer value
// So, we convert each integer value to char
c = (char)a;
System.out.print(c);
}
}
catch(Exception e)
{
// In case of error
e.printStackTrace();
System.out.println("ERROR Occurs ");
}
finally
{
// Closing the streams, Once the End of Input Stream is reached
if(geekinput != null)
geekinput.close();
if(geekline != null)
geekline.close();
}
}
}
Note :
The following Java Code won't run here as we can't access any file on online IDE.
So, copy the program to your system and run it there.
The ABC.txt file used in the program contains :
Hello
Geeks.
Explaining
read() method
Output :
Hello
Geeks.
Explaining
read() method
- getLineNumber() : java.io.LineNumberInputStream.getLineNumber() returns number of current line.
Syntax :
public int getLineNumber()
Parameters :
-------
Return :
no. of current line
Implementation :
Java
// Java program illustrating the working of getLineNumber() method
import java.io.*;
public class NewClass
{
public static void main(String[] args) throws IOException
{
// LineNumberInputStream & FileInputStream initially null
LineNumberInputStream geekline = null;
FileInputStream geekinput = null;
try
{
char c;
int a, b;
// New InputStream : 'ABC' is created
geekinput = new FileInputStream("ABC.txt");
geekline = new LineNumberInputStream(geekinput);
while((a = geekline.read()) != -1)
{
// So, we convert each integer value to char
c = (char)a;
// Use of getLineNumber() : to print line no.
a = geekline.getLineNumber();
System.out.println(" At line : " + a);
System.out.print(c);
}
a = geekline.getLineNumber();
System.out.println(" at line: " + a);
}
catch(Exception e)
{
// In case of error
e.printStackTrace();
System.out.println("ERROR Occurs ");
}
finally
{
// Closing the streams, Once the End of Input Stream is reached
if(geekinput != null)
geekinput.close();
if(geekline != null)
geekline.close();
}
}
}
Note :
The following Java Code won't run here as we can't access any file on online IDE.
So, copy the program to your system and run it there.
The ABC.txt file used in the program contains :
no. of
lines
Output :
At line : 0
n At line : 0
o At line : 0
. At line : 0
At line : 0
o At line : 0
f At line : 1
At line : 1
l At line : 1
i At line : 1
n At line : 1
e At line : 1
s at line: 1
- available() : java.io.LineNumberInputStream.available() returns the number of bytes that can be read from the Input Stream without blocking.
Syntax :
public int available()
Parameters :
-------
Return :
returns the no. of bytes that can be read from the Input Stream.
Exception:
IOException : in case I/O error occurs
Implementation :
Java
// Java program illustrating the working of available() method
import java.io.*;
public class NewClass
{
public static void main(String[] args) throws IOException
{
// LineNumberInputStream & FileInputStream initially null
LineNumberInputStream geekline = null;
FileInputStream geekinput = null;
try{
char c;
int a, b;
// New InputStream : 'ABC' is created
geekinput = new FileInputStream("ABC.txt");
geekline = new LineNumberInputStream(geekinput);
while((a = geekline.read()) != -1)
{
// So, we convert each integer value to char
c = (char)a;
// Use of available method : return no. of bytes that can be read
a = geekline.available();
System.out.println(c + " Bytes available : " + a);
}
}
catch(Exception e)
{
// In case of error
e.printStackTrace();
System.out.println("ERROR Occurs ");
}
finally
{
// Closing the streams, Once the End of Input Stream is reached
if(geekinput != null)
geekinput.close();
if(geekline != null)
geekline.close();
}
}
}
Note :
The following Java Code won't run here as we can't access any file on online IDE.
So, copy the program to your system and run it there.
The ABC.txt file used in the program contains :
available
Output :
a Bytes available : 4
v Bytes available : 3
a Bytes available : 3
i Bytes available : 2
l Bytes available : 2
a Bytes available : 1
b Bytes available : 1
l Bytes available : 0
e Bytes available : 0
- setLineNumber() : java.io.LineNumberInputStream.setLineNumber(int arg) assigns line number to the argument we want.
Syntax :
public void setLineNumber(int arg)
Parameters :
arg : line number to assign
Return :
void
Exception:
-----
Implementation :
Java
// Java program illustrating the working of setLineNumber() method
import java.io.*;
public class NewClass
{
public static void main(String[] args) throws IOException
{
// LineNumberInputStream & FileInputStream initially null
LineNumberInputStream geekline = null;
FileInputStream geekinput = null;
try{
char c;
int a, b = 0;
// New InputStream : 'ABC' is created
geekinput = new FileInputStream("ABC.txt");
geekline = new LineNumberInputStream(geekinput);
while((a = geekline.read()) != -1)
{
// So, we convert each integer value to char
c = (char)a;
// Use of setLineNumber() : to set the line no.
geekline.setLineNumber(100 + b);
// getLineNumber() : returning the current line no.
a = geekline.getLineNumber();
System.out.println(c + " Line No. Set : " + a);
b++;
}
}
catch(Exception e)
{
// In case of error
e.printStackTrace();
System.out.println("ERROR Occurs ");
}
finally
{
// Closing the streams, Once the End of Input Stream is reached
if(geekinput != null)
geekinput.close();
if(geekline != null)
geekline.close();
}
}
}
Note :
The following Java Code won't run here as we can't access any file on online IDE.
So, copy the program to your system and run it there.
The ABC.txt file used in the program contains :
LineNumber
Output :
L Line No. Set : 100
i Line No. Set : 101
n Line No. Set : 102
e Line No. Set : 103
N Line No. Set : 104
u Line No. Set : 105
m Line No. Set : 106
b Line No. Set : 107
e Line No. Set : 108
r Line No. Set : 109
- skip() : java.io.LineNumberInputStream.skip(long arg) skips and discards 'arg' bytes from Input Stream data. The skip method of LineNumberInputStream creates a byte array and then repeatedly reads into it until n bytes have been read or the end of the stream has been reached.
Syntax :
public long skip(long arg)
Parameters :
arg : no. of bytes of Input Stream data to skip.
Return :
no. of bytes to be skipped
Exception:
IOException : in case I/O error occurs
Implementation :
Java
// Java program illustrating the working of setLineNumber() method
import java.io.*;
public class NewClass
{
public static void main(String[] args) throws IOException
{
// LineNumberInputStream & FileInputStream initially null
LineNumberInputStream geekline = null;
FileInputStream geekinput = null;
try{
char c;
int a, b = 0;
// New InputStream : 'ABC' is created
geekinput = new FileInputStream("ABC.txt");
geekline = new LineNumberInputStream(geekinput);
while((a = geekline.read()) != -1)
{
// So, we convert each integer value to char
c = (char)a;
// skip() : to skip and discard 'arg' bytes
// Here skip() will skip and discard 3 bytes.
geekline.skip(3);
System.out.println(c);
}
}
catch(Exception e)
{
// In case of error
e.printStackTrace();
System.out.println("ERROR Occurs ");
}
finally{
// Closing the streams, Once the End of Input Stream is reached
if(geekinput != null)
geekinput.close();
if(geekline != null)
geekline.close();
}
}
}
Note :
The following Java Code won't run here as we can't access any file on online IDE.
So, copy the program to your system and run it there.
The ABC.txt file used in the program contains :
Program
Explaining
Skip() method
Output :"
P
r
E
a
n
k
)
t
- read() : java.io.LineNumberInputStream.read(byte[] buffer, int offset, int maxlen) reads up to 'maxlen' bytes from Input Stream into bytes.
Syntax :
public int read(byte[] buffer, int offset, int maxlen)
Parameters :
buffer : buffer whose data to read
offset : starting offset of the data
maxlen : max. no. of bytes to read
Return :
total no. of bytes, else return -1 if End of Input Stream is identified
Exception:
IOException : in case I/O error occurs
Implementation :
Java
// Java program illustrating the working of read() method
import java.io.*;
public class NewClass
{
public static void main(String[] args) throws IOException
{
// LineNumberInputStream & FileInputStream initially null
LineNumberInputStream geekline = null;
FileInputStream geekinput = null;
try{
char c;
int a;
// New InputStream : 'ABC' is created
geekinput = new FileInputStream("ABC.txt");
geekline = new LineNumberInputStream(geekinput);
// read() method returning Bytes of Input Stream as integer
// '-1' indicating to read till end Of Input Stream
while((a=geekline.read())!=-1)
{
// Since read() method returns Integer value
// So, we convert each integer value to char
c = (char)a;
System.out.print(c);
}
}
catch(Exception e)
{
// In case of error
e.printStackTrace();
System.out.println("ERROR Occurs ");
}
finally
{
// Closing the streams, Once the End of Input Stream is reached
if(geekinput != null)
geekinput.close();
if(geekline != null)
geekline.close();
}
}
}
Note :
The following Java Code won't run here as we can't access any file on online IDE.
So, copy the program to your system and run it there.
The ABC.txt file used in the program contains :
Read() method
what the method does is offset = r and maxlen = 5... so ---i.e. 3 offset, then 5 bytes i.e. Read(, then again offset, so --
Output :
The number of char read: 5
---Read(--
- mark() : Java.io.LineNumberInputStream.mark(int arg) marks the current position of the input stream. It sets readlimit i.e. maximum number of bytes that can be read before mark position becomes invalid.
Syntax :
public void mark(int arg)
Parameters :
arg : integer specifying the read limit of the input Stream
Return :
void
- reset() : Java.io.LineNumberInputStream.reset() is invoked by mark() method. It re-positions the input stream to the marked position.
Syntax :
public void reset()
Parameters :
----
Return :
void
Exception :
-> IOException : If I/O error occurs.
Java Program explaining LineNumberInputStream Class methods : reset() and mark()
Java
// Java program illustrating the working of LineNumberInputStream method
// mark() and reset()
import java.io.*;
public class NewClass
{
public static void main(String[] args) throws Exception
{
LineNumberInputStream geekline = null;
FileInputStream geek = null;
try{
geek = new FileInputStream("GEEKS.txt");
geekline = new LineNumberInputStream(geek);
// read() method : reading and printing Characters one by one
System.out.println("Char : " + (char)geekline.read());
System.out.println("Char : " + (char)geekline.read());
System.out.println("Char : " + (char)geekline.read());
// mark() : read limiting the 'geek' input stream
geekline.mark(0);
// skip() : it results in reading of 'e' in G'e'eeks
geekline.skip(1);
System.out.println("skip() method comes to play");
System.out.println("mark() method comes to play");
System.out.println("Char : " + (char)geekline.read());
System.out.println("Char : " + (char)geekline.read());
System.out.println("Char : " + (char)geekline.read());
boolean check = geekline.markSupported();
if(geekline.markSupported())
{
// reset() method : repositioning the stream to marked positions.
geekline.reset();
System.out.println("reset() invoked");
System.out.println("Char : " + (char)geekline.read());
System.out.println("Char : " + (char)geekline.read());
}
else
{
System.out.println("reset() method not supported.");
}
System.out.println("geekline.markSupported() supported reset() : "
+ check);
}
catch(Exception except)
{
// in case of I/O error
except.printStackTrace();
}
finally
{
// releasing the resources back to the GarbageCollector when closes
if(geek != null)
geek.close();
if(geekline != null)
geekline.close();
}
}
}
Note :
This code won't run on online IDE as no such file is present here.
You can run this code on your System to check the working.
ABC.txt file used in the code has
HelloGeeks
Output :
Char : H
Char : e
Char : l
skip() method comes to play
mark() method comes to play
Char : o
Char : G
Char : e
reset() method not supported.
geekline.markSupported() supported reset() : false
Similar Reads
Java Tutorial
Java is a high-level, object-oriented programming language used to build applications across platformsâfrom web and mobile apps to enterprise software. It is known for its Write Once, Run Anywhere capability, meaning code written in Java can run on any device that supports the Java Virtual Machine (
11 min read
Java OOP(Object Oriented Programming) Concepts
Java Object-Oriented Programming (OOPs) is a fundamental concept in Java that every developer must understand. It allows developers to structure code using classes and objects, making it more modular, reusable, and scalable.The core idea of OOPs is to bind data and the functions that operate on it,
13 min read
Java Interview Questions and Answers
Java is one of the most popular programming languages in the world, known for its versatility, portability, and wide range of applications. Java is the most used language in top companies such as Uber, Airbnb, Google, Netflix, Instagram, Spotify, Amazon, and many more because of its features and per
15+ min read
SQL Commands | DDL, DQL, DML, DCL and TCL Commands
SQL commands are crucial for managing databases effectively. These commands are divided into categories such as Data Definition Language (DDL), Data Manipulation Language (DML), Data Control Language (DCL), Data Query Language (DQL), and Transaction Control Language (TCL). In this article, we will e
7 min read
Arrays in Java
Arrays in Java are one of the most fundamental data structures that allow us to store multiple values of the same type in a single variable. They are useful for storing and managing collections of data. Arrays in Java are objects, which makes them work differently from arrays in C/C++ in terms of me
15+ min read
TCP/IP Model
The TCP/IP model (Transmission Control Protocol/Internet Protocol) is a four-layer networking framework that enables reliable communication between devices over interconnected networks. It provides a standardized set of protocols for transmitting data across interconnected networks, ensuring efficie
7 min read
Inheritance in Java
Java Inheritance is a fundamental concept in OOP(Object-Oriented Programming). It is the mechanism in Java by which one class is allowed to inherit the features(fields and methods) of another class. In Java, Inheritance means creating new classes based on existing ones. A class that inherits from an
13 min read
Java Exception Handling
Exception handling in Java allows developers to manage runtime errors effectively by using mechanisms like try-catch block, finally block, throwing Exceptions, Custom Exception handling, etc.An Exception is an unwanted or unexpected event that occurs during the execution of a program (i.e., at runti
10 min read
Collections in Java
Any group of individual objects that are represented as a single unit is known as a Java Collection of Objects. In Java, a separate framework named the "Collection Framework" has been defined in JDK 1.2 which holds all the Java Collection Classes and Interface in it. In Java, the Collection interfac
15+ min read
Basics of Computer Networking
A computer network is a collection of interconnected devices that share resources and information. These devices can include computers, servers, printers, and other hardware. Networks allow for the efficient exchange of data, enabling various applications such as email, file sharing, and internet br
14 min read