Read Large Text File Java Line by Line
Read a Large Text File Line by Line in Java
-
BufferedReader
to Read File Line by Line in Java -
Stream
to Read File Line past Line in Coffee -
Scanner
to Read File Line past Line in Java
This tutorial discusses methods to read a large text file line past line efficiently in Java.
There are numerous methods to read a text file in Java. Withal, this tutorial is specific to reading large text files, and we will hash out the three well-nigh efficient methods to read large text files quickly.
BufferedReader
to Read File Line past Line in Java
BufferedReader
class in Coffee reads text from a given character-input stream, buffering characters to provide for the efficient reading of characters, arrays, and lines. This method provides an efficient line by line reading for the input files with a considerably large file size.
The below example illustrates the use of BufferedReader
to read a txt
file and output its content line by line.
import java.io.*; public class Primary { public static void primary(String[] args) { Cord file = "my-file.txt"; try(BufferedReader br = new BufferedReader(new FileReader(file))) { String line; while ((line = br.readLine()) != null) { Organization.out.println(line); } } catch (IOException e) { Organisation.out.println("An error occurred."); e.printStackTrace(); } } }
Stream
to Read File Line by Line in Java
Java 8 and in a higher place users can also use Stream
to read large files line by line. The below case illustrates the employ of Stream
to read a txt
file and output its content line past line.
import java.io.*; import java.nio.file.*; import java.util.stream.*; public class Chief { public static void main(String[] args) { String file = "my-file.txt"; attempt (Stream<Cord> stream = Files.lines(Paths.go(fileName))) { stream.forEach(Arrangement.out::println); } take hold of (IOException e) { Organization.out.println("An error occurred."); e.printStackTrace(); } } }
The above 2 methods discussed would read the input file line by line instead of reading the whole file into retentivity. Therefore these two methods are highly efficient if we accept a huge file that can not be fully read into the memory.
Nonetheless, if our memory is large enough to read the input file fully, we can besides endeavour the beneath method.
Scanner
to Read File Line past Line in Java
Scanner
grade in Coffee is a simple text scanner which can parse primitive types and strings using regular expressions. Scanner(File source)
reads the complete file into retentiveness and then processes it line past line.
The example below illustrates the utilize of Scanner
to read a txt
file and output its content line by line.
import java.io.*; import java.util.*; public grade Main { public static void main(String [] args) throws IOException { String fileName = "my-file.txt"; Scanner scan = new Scanner(new File(fileName)); while(scan.hasNextLine()){ String line = scan.nextLine(); Arrangement.out.println(line); } } }
We discussed three methods to read a large text file in Coffee and process information technology line by line. Each method had some constraints and advantages that nosotros must consider while deciding which method to use in a particular scenario.
Write for us
DelftStack manufactures are written past software geeks like yous. If you also would like to contribute to DelftStack past writing paid articles, you tin can check the write for us page.
Related Article - Java File
Source: https://www.delftstack.com/howto/java/how-to-read-a-large-text-file-line-by-line-in-java/