Java.io - SPLessons

Java.io LineNumberReader

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

Java.io LineNumberReader

Java.io LineNumberReader

shape Introduction

The Java.io LineNumberReader is the class used to keep track of present line numbers of the characters. The line number reader is subclass to reader. When calling getLineNumber() method the Java.io LineNumberReader gives the line number for the present characters. By using the setLineNumber() method you can set the line number for the present character. A line represents the sequence of characters which ends with '/r' and for a new line character '/n'.

Class Declaration

shape Declaration

Java.io LineNumberReader class is declared below: Public class LineNumberReader extends BufferedReader

shape Fields

Java.io LineNumberReader class fields are:
  • Protected Object Lock : For synchronize operations this object is used in FilterReader.

Class Constructors

shape Table

Constructor Description
LineNumberReader(Reader in) The function of this constructor is to create the LineNumberReader instance and used to read from input stream.
LineNumberReader(Reader in, int size) The function of this constructor is to create the LineNumberReader instance and used to read characters based on specified size of buffer.

Class Methods

shape Table

Method Description
void close () The function of this method is to close the present stream and associated to input stream systems resources are released.
void setLineNumber(int LineNumber) The function of this method is to set line number to the represented argument.
int read(Char[] cbuf, int off, int n) By using this method it reads the data up to ‘n’ length from the character stream into an array of characters.
Long skip (long m) The function of this method is to skip the text or characters in this stream of ‘ m ‘ characters.
int read() The function of this method is to read  only one character.

Inherited Methods

shape Description

From the following classes, methods are inherited to the reader class.
  • Java.io.Object
  • Java.io.Reader
  • Java.io.BufferedReader

shape Examples

Usage of Long skip (long m) method. [c]import java.io.FileReader; import java.io.IOException; import java.io.LineNumberReader; public class LineNumberReader { public static void main(String[] args) throws IOException { FileReader fr = null; LineNumberReader lnr = null; int i; char c; try{ // creating a new reader instance fr = new FileReader("D:/test.txt"); lnr = new LineNumberReader(fr); // To read till the end of the stream while((i=lnr.read())!=-1) { // To skip one character lnr.skip(1); // To convert integer to character c=(char)i; // To print character System.out.print(c); } }catch(Exception e){ // If any I/O error occurs e.printStackTrace(); }finally{ // It closes all the stream and releases system resources if(fr!=null) fr.close(); if(lnr!=null) lnr.close(); } } }[/c] Output The result will be as follows. The result content will be available in D:/test.txt. [c]SLSOS[/c] Usage of int read(Char[] cbuf, int off, int n) method. [c]import java.io.FileReader; import java.io.IOException; import java.io.LineNumberReader; public class LineNumberReader { public static void main(String[] args) throws IOException { FileReader fr = null; LineNumberReader lnr = null; int i; char[] cbuf = new char[9]; try{ // creating a new reader instance fr = new FileReader("D:/test.txt"); lnr = new LineNumberReader(fr); // To read characters into the buffer i=lnr.read(cbuf, 0, 9); System.out.println("Number of characters read: "+i); // For to represent each character in the buffer for(char c:cbuf) { // If char is empty if((int)c==0) c='-'; // To print character System.out.print(c); } }catch(Exception e){ // If any error occurs e.printStackTrace(); }finally{ // It closes the stream and releases system resources if(fr!=null) fr.close(); if(lnr!=null) lnr.close(); } } }[/c] Output The result will be as follows. The result content will be available in D:/test.txt. [c]Number of characters read: 9 SPLESSONS[/c]

Summary

shape Key Points

  • The line number reader is the class used to keep track of present line numbers of the characters.
  • The line number reader is subclass to reader.
  • When calling getLineNumber() method the line number reader gives the line number for the present characters.
  • Using the setLineNumber() method you can set the line number for the present character.