Java LineNumberReader Tutorial with Examples
1. LineNumberReader
No ADS
LineNumberReader is a subclass of BufferedReader, which supports tracking the line number of lines of text. Line numbering starts from zero.
Whenever LineNumberReader encounters a line break, the line number will increase by 1. The getLineNumber() method returns the current line number. You can also use the setLineNumber(int) method to set the line number for the current line of text.
LineNumberReader constructors
LineNumberReader(Reader in)
LineNumberReader(Reader in, int sz)
test.txt
Java Tutorials:
Java Reader Tutorial
Java Writer Tutorial
Example:
LineNumberReaderEx1.java
package org.o7planning.linenumberreader.ex;
import java.io.FileReader;
import java.io.IOException;
import java.io.LineNumberReader;
import java.io.Reader;
public class LineNumberReaderEx1 {
public static void main(String[] args) throws IOException {
try(Reader reader = new FileReader("/Volumes/Data/test/test.txt");
LineNumberReader lineNumberReader = new LineNumberReader(reader)){
System.out.println("Start Line Number: " + lineNumberReader.getLineNumber());
System.out.println(" ----- ");
String line = null; ;
while((line = lineNumberReader.readLine()) != null) {
System.out.println("Line Number: " + lineNumberReader.getLineNumber());
System.out.println(" Line Content: " + line);
}
}
}
}
Output:
Start Line Number: 0
-----
Line Number: 1
Line Content: Java Tutorials:
Line Number: 2
Line Content:
Line Number: 3
Line Content: Java Reader Tutorial
Line Number: 4
Line Content: Java Writer Tutorial
2. setLineNumber(int)
No ADS
Set the line number for the current line of text.
public void setLineNumber(int lineNumber)
Example:
test2.txt
AAAAA
BBBBB
CCCCC
DDDDD
EEEEE
LineNumberReader_setLineNumber_ex1.java
package org.o7planning.linenumberreader.ex;
import java.io.FileReader;
import java.io.IOException;
import java.io.LineNumberReader;
import java.io.Reader;
public class LineNumberReader_setLineNumber_ex1 {
public static void main(String[] args) throws IOException {
try(Reader reader = new FileReader("/Volumes/Data/test/test2.txt");
LineNumberReader lineNumberReader = new LineNumberReader(reader)){
System.out.println("Set current line number to 2");
lineNumberReader.setLineNumber(2);
System.out.println("Line Number after set: " + lineNumberReader.getLineNumber());
System.out.println(" ----- ");
String line = null; ;
while((line = lineNumberReader.readLine()) != null) {
System.out.println("Line Number: " + lineNumberReader.getLineNumber());
System.out.println(" Line Content: " + line);
}
}
}
}
Output:
Set current line number to 2
Line Number after set: 2
-----
Line Number: 3
Line Content: AAAAA
Line Number: 4
Line Content: BBBBB
Line Number: 5
Line Content: CCCCC
Line Number: 6
Line Content: DDDDD
Line Number: 7
Line Content: EEEEE
No ADS
Java IO Tutorials
- Java CharArrayWriter Tutorial with Examples
- Java FilterReader Tutorial with Examples
- Java FilterWriter Tutorial with Examples
- Java PrintStream Tutorial with Examples
- Java BufferedReader Tutorial with Examples
- Java BufferedWriter Tutorial with Examples
- Java StringReader Tutorial with Examples
- Java StringWriter Tutorial with Examples
- Java PipedReader Tutorial with Examples
- Java LineNumberReader Tutorial with Examples
- Java PrintWriter Tutorial with Examples
- Java IO Binary Streams Tutorial with Examples
- Java IO Character Streams Tutorial with Examples
- Java BufferedOutputStream Tutorial with Examples
- Java ByteArrayOutputStream Tutorial with Examples
- Java DataOutputStream Tutorial with Examples
- Java PipedInputStream Tutorial with Examples
- Java OutputStream Tutorial with Examples
- Java ObjectOutputStream Tutorial with Examples
- Java PushbackInputStream Tutorial with Examples
- Java SequenceInputStream Tutorial with Examples
- Java BufferedInputStream Tutorial with Examples
- Java Reader Tutorial with Examples
- Java Writer Tutorial with Examples
- Java FileReader Tutorial with Examples
- Java FileWriter Tutorial with Examples
- Java CharArrayReader Tutorial with Examples
- Java ByteArrayInputStream Tutorial with Examples
- Java DataInputStream Tutorial with Examples
- Java ObjectInputStream Tutorial with Examples
- Java InputStreamReader Tutorial with Examples
- Java OutputStreamWriter Tutorial with Examples
- Java InputStream Tutorial with Examples
- Java FileInputStream Tutorial with Examples
Show More