Java - LineNumberReader read() method
Description
The Java LineNumberReader read() method reads a single character at a time from the stream and automatically tracks line numbers. Unlike readLine(), which reads entire lines, read() processes one character at a time.
Declaration
Following is the declaration for java.io.LineNumberReader.read() method −
public int read()
Parameters
NA
Return Value
The method returns the character read, or -1 if the end of the stream has been reached.
Exception
IOException − If an I/O error occurs.
Example - Usage of LineNumberReader read() method
The following example shows the usage of Java LineNumberReader read() method.
LineNumberReaderDemo.java
package com.tutorialspoint;
import java.io.FileReader;
import java.io.IOException;
import java.io.LineNumberReader;
public class LineNumberReaderDemo {
public static void main(String[] args) throws IOException {
FileReader fr = null;
LineNumberReader lnr = null;
int i;
char c;
try {
// create new reader
fr = new FileReader("test.txt");
lnr = new LineNumberReader(fr);
while((i = lnr.read())!=-1) {
// converts int to char
c = (char)i;
// prints character
System.out.println(c);
}
} catch(Exception e) {
// if any error occurs
e.printStackTrace();
} finally {
// closes the stream and releases system resources
if(fr!=null)
fr.close();
if(lnr!=null)
lnr.close();
}
}
}
Output(Assuming test.txt contains "ABCDE")
Let us compile and run the above program, this will produce the following result−
A B C D E
Example - Reading Characters One by One
The following example shows the usage of Java LineNumberReader read() method. This example reads characters from a LineNumberReader one at a time and prints their ASCII values along with the current line number.
LineNumberReaderDemo.java
package com.tutorialspoint;
import java.io.IOException;
import java.io.LineNumberReader;
import java.io.StringReader;
public class LineNumberReaderDemo {
public static void main(String[] args) {
String text = "Hello\nWorld\nJava";
LineNumberReader lnr = new LineNumberReader(new StringReader(text));
try {
int character;
while ((character = lnr.read()) != -1) { // Read one character at a time
System.out.println("Character: " + (char) character + ", Line Number: " + lnr.getLineNumber());
}
lnr.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
Output
Let us compile and run the above program, this will produce the following result−
Character: H, Line Number: 0 Character: e, Line Number: 0 Character: l, Line Number: 0 Character: l, Line Number: 0 Character: o, Line Number: 0 Character: , Line Number: 1 Character: W, Line Number: 1 Character: o, Line Number: 1 Character: r, Line Number: 1 Character: l, Line Number: 1 Character: d, Line Number: 1 Character: , Line Number: 2 Character: J, Line Number: 2 Character: a, Line Number: 2 Character: v, Line Number: 2 Character: a, Line Number: 2
Explanation
The read() method reads characters one at a time.
Line number starts at 0 and increments when a newline (\n) is encountered.
The program prints the character and its corresponding line number.
Example - Reading a File Character by Character
The following example shows the usage of Java LineNumberReader read() method. This example reads characters from a file (example.txt) using LineNumberReader.read() and tracks the line numbers.
LineNumberReaderDemo.java
package com.tutorialspoint;
import java.io.IOException;
import java.io.LineNumberReader;
import java.io.FileReader;
public class LineNumberReaderDemo {
public static void main(String[] args) {
try {
// Create a LineNumberReader for a file
LineNumberReader lnr = new LineNumberReader(new FileReader("example.txt"));
int character;
while ((character = lnr.read()) != -1) { // Read until end of file
System.out.print((char) character); // Print character
if (character == '\n') { // If newline, print line number
System.out.println("Line Number: " + lnr.getLineNumber());
}
}
lnr.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
Output(Assuming example.txt contains multiple lines)
Let us compile and run the above program, this will produce the following result−
Hello Line Number: 1 World Line Number: 2 Java Programming Line Number: 3
Explanation
read() reads one character at a time from the file.
When a newline (\n) is encountered, it prints the updated line number.
Line numbers are tracked automatically by LineNumberReader.