How to read text file line by line in Java
- Details
- Written by Nam Ha Minh
- Last Updated on 28 July 2019   |   Print Email
In Java File I/O programming, the classes BufferedReader and LineNumberReader allows reading a sequence of lines from a text file, in a line-by-line fashion, by using their readLine() method. The LineNumberReader is a subclass of the BufferedReader class. The only difference is that, the LineNumberReader class keeps track of the current line number, whereas the BufferedReader class does not.
Reading all lines from a text file using the BufferedReader class is as easy as follows:
String filePath = "Path/To/Your/Text/File.txt"; try { BufferedReader lineReader = new BufferedReader(new FileReader(filePath)); String lineText = null; while ((lineText = lineReader.readLine()) != null) { System.out.println(lineText); } lineReader.close(); } catch (IOException ex) { System.err.println(ex); }
The above code snippet takes a given text file, reads every line and prints content of each line to the console output. It’s also common to read and put all the lines into a collection (e.g. an array list) for processing later, like this:
BufferedReader lineReader = new BufferedReader(new FileReader(filePath)); String lineText = null; List<String> listLines = new ArrayList<String>(); while ((lineText = lineReader.readLine()) != null) { listLines.add(lineText); } lineReader.close();
Using the LineNumberReader class is similar to the BufferedReader class, for example:
String filePath = "Path/To/Your/Text/File.txt"; try { LineNumberReader lineReader = new LineNumberReader(new FileReader(filePath)); String lineText = null; while ((lineText = lineReader.readLine()) != null) { System.out.println(lineReader.getLineNumber() + ": " + lineText); } lineReader.close(); } catch (IOException ex) { System.err.println(ex); }
The LineNumberReader class provides the getLineNumber() method which returns the current line number, so we can use it for checking conditions on line numbers, for example:
- Printing only the even lines:
while ((lineText = lineReader.readLine()) != null) { int lineNumber = lineReader.getLineNumber(); if (lineNumber % 2 == 0) { System.out.println(lineNumber + ": " + lineText); } }
- Printing only the lines in a specific range:
while ((lineText = lineReader.readLine()) != null) { int lineNumber = lineReader.getLineNumber(); if (lineNumber >= 50 && lineNumber <= 100) { System.out.println(lineNumber + ": " + lineText); } }
NOTES:The LineNumberReader class also provides the setLineNumber() method but it doesn’t change to the current position in the file stream. It only changes the value that will be returned by the getLineNumber() method.
API References:
Related File IO Tutorials:
Other Java File IO Tutorials:
- How to list files and directories in a directory in Java
- Java IO - Common File and Directory Operations Examples
- Java Serialization Basic Example
- Understanding Java Externalization with Examples
- How to execute Operating System Commands in Java
- 3 ways for reading user's input from console in Java
- File change notification example with Watch Service API
- Java Scanner Tutorial and Code Examples
- How to compress files in ZIP format in Java
- How to extract ZIP file in Java
Comments
pls provide
getline num set line num examples