
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Read a Large Text File Line by Line in Java
This article includes the way to read text files line by line in Java. Here, the three different methods are explained with examples. Storing a file in text format is important especially when data from one structured /unstructured form is transferred to another form. Therefore, basic file transfer systems integrate the txt file reading and writing process as their application components. Therefore, how to use text file reading and writing methods is also important for making the parsers.
Multiple Approaches
The given problem statement is solved in three different approaches ?
Let's see the program along with its output sequentially.
Note ? These programs will not give the expected result on any online Java compiler. Online editors will not access your local system's file path.
By using the BufferedReader
In this approach, the buffer is provided by the BufferedReader into the FileReader. Its performance is effective as it uses larger block reading rather than a single character reading at a time.
-
Specify the file to be read.
-
Read the file lines as per distinct approaches which means the file is read as a block.
-
Print the lines read from the file on the display/screen.
Example
import java.io.BufferedReader; import java.io.FileReader; import java.io.IOException; public class Read { public static void main(String[] args) { // making the BufferedReader object BufferedReader b_reader; try { // Reading the sample.txt file b_reader = new BufferedReader(new FileReader("sample.txt")); //reading line by line String ln = b_reader.readLine(); while (ln != null) { //printing the line System.out.println(ln); ln = b_reader.readLine(); } b_reader.close(); } catch (IOException e) { e.printStackTrace(); } } }
Output
C:\java\javaprgstu\filehandling\File Handling Tutorial\Code\1_ReadFiles>javac Read.java C:\java\javaprgstu\filehandling\File Handling Tutorial\Code\1_ReadFiles>java Read Block1 This is an experimental file. Inserting the line 100 This is an experimental file. Inserting the line 101 This is an experimental file. Inserting the line 102 ?.. This is an experimental file. Inserting the line 277 Block2 This is an experimental file. Inserting the line 100 This is an experimental file. Inserting the line 101 ?. This is an experimental file. Inserting the line 277 Block3 This is an experimental file. Inserting the line 100 ? This is an experimental file. Inserting the line 277
The Sample File used contains 3 Blocks of Lines with 177 lines in each block
Explanation
BufferReader is not the same as BufferedInputStream because BufferedReader reads characters while BufferedInputStream reads raw bytes. The reader is the superclass of BufferedReader and the superclass of Reader class is the Object class. The BufferedReader class supports two constructors. Using these constructors bufferedReader instances are created. One constructor accepts the reader instance and the other constructor accepts the reader instance and the buffer size.
By using the FileInputStream
Java Applications can use FileInputStream to read data from the text files. In this approach, the file content is read as a bytes stream.
-
Specify the file to be read.
-
Retrieve the file lines and the file is read as a bytes.
-
Display the lines read from the file on the screen.
Example
import java.io.FileInputStream; import java.io.InputStream; import java.nio.charset.StandardCharsets; import java.util.Scanner; import java.io.FileNotFoundException; public class Read2 { public static void main(String[] args) // exception handling throws FileNotFoundException{ String path = "C:\java\javaprgstu\filehandling\File Handling Tutorial\Code\1_ReadFiles\sample.txt"; //passing the file as a parameter to FileInputStream InputStream ins = new FileInputStream(path); // using Scanner scn to read the file input stream try (Scanner scn = new Scanner( ins, StandardCharsets.UTF_8.name())) { while (scn.hasNextLine()) { //printing line by line System.out.println(scn.nextLine()); } } } }
Output
C:\java\javaprgstu\filehandling\File Handling Tutorial\Code\1_ReadFiles>javac Read2.java C:\java\javaprgstu\filehandling\File Handling Tutorial\Code\1_ReadFiles>java Read2 Block1 This is an experimental file. Inserting the line 100 This is an experimental file. Inserting the line 101 This is an experimental file. Inserting the line 102 This is an experimental file. Inserting the line 103 This is an experimental file. Inserting the line 104 This is an experimental file. Inserting the line 105 This is an experimental file. Inserting the line 106 ??????????????
The Sample File used contains 3 Blocks of Lines with 177 lines in each block
Explanation
InputStream is the superclass of FileInputStream. The FileInputStream class supports three constructors. Using these constructors FileInputStream instances are created. A constructor accepts the String as a parameter and the other constructor accepts the file object as a parameter.
By using the FileReader
Java Applications can use FileReader to read data from the text files. In this case, the content of the file is read as a stream of characters.
-
Set the path of the file.
-
Read the file lines and the file is read as a character.
-
Display the lines read from the file on the display/screen.
Example
import java.io.File; import java.io.FileReader; import java.io.IOException; public class Read3{ public static void main(String[] args){ //Set the name of the file to read File fileone = new File("sample.txt"); // make frd as a FileReader object try (FileReader frd = new FileReader(fileone)){ int content1; while ((content1 = frd.read()) != -1) { System.out.print((char) content1); } } catch (IOException e) { e.printStackTrace(); } } }
Output
C:\java\javaprgstu\filehandling\File Handling Tutorial\Code\1_ReadFiles>javac Read3.java C:\java\javaprgstu\filehandling\File Handling Tutorial\Code\1_ReadFiles>java Read3 Block1 This is an experimental file. Inserting the line 100 This is an experimental file. Inserting the line 101 This is an experimental file. Inserting the line 102 This is an experimental file. Inserting the line 103 This is an experimental file. Inserting the line 104 This is an experimental file. Inserting the line 105 This is an experimental file. Inserting the line 106 This is an experimental file. Inserting the line 107 ???????????????.
The Sample File used contains 3 Blocks of Lines with 177 lines in each block
Explanation
The reader is the superclass of FileReader and the superclass of Reader class is the Object class. Specify the proper path of the file to read the large text.
Conclusion
In the above article, three different approaches are used to read the lines from a file using Java language. The first approach reads the lines as blocks of characters. Another approach reads the lines as a byte stream and the third approach reads the lines as characters.