Java Program to Read a Large Text File Line by Line
Last Updated :
26 Aug, 2022
As we are well verse with this topic let us put more stress in order to figure out minute differences between them. Here we are supposed to read from a file on the local directory where a text file is present say it be 'gfg.txt'. Let the content inside the file be as shown below:
Geeks for Geeks.
A computer science portal.
Welcome to this portal.
Hello Geek !!!
Note: Keep a check that prior doing anything first create a file on the system repository to deal with our program\writing a program as we will be accessing the same directory through our programs.
Methods:
- Using Scanner class
- Using BufferedReaader class
Method 1: Using Scanner class
Scanner is a class in java.util package used for obtaining the input of the primitive types like int, double, etc. and strings. It is the easiest way to read input in a Java program, though not very efficient if you want an input method for scenarios where time is a constraint like in competitive programming. Scanner class is used to read the large file line by line. A Scanner breaks its input into tokens, which by default matches the whitespace.
Example
Java
// Java Program to Read a Large Text File Line by Line
// Using Scanner class
// Importing required classes
import java.io.*;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.util.Scanner;
// Main class
public class GFG {
// Main driver method
public static void main(String[] args)
throws FileNotFoundException
{
// Declaring and initializing the string with
// custom path of a file
String path = "C:\\Users\\HP\\Desktop\\gfg.txt";
// Creating an instance of Inputstream
InputStream is = new FileInputStream(path);
// Try block to check for exceptions
try (Scanner sc = new Scanner(
is, StandardCharsets.UTF_8.name())) {
// It holds true till there is single element
// left in the object with usage of hasNext()
// method
while (sc.hasNextLine()) {
// Printing the content of file
System.out.println(sc.nextLine());
}
}
}
}
Output:
Geeks for Geeks.
A computer science portal.
Welcome to this portal.
Hello Geek !!!
Method 2: Using BufferedReader class
BufferedReader is used to read the file line by line. Basically, BufferedReader() is used for the processing of large files. BufferedReader is very efficient for reading.
Note: Specify the size of the BufferReader or keep that size as a Default size of BufferReader. The default size of BufferReader is 8KB.
Syntax:
BufferedReader in = new BufferedReader(Reader in, int size);
Example:
Java
// Java Program to Read a Large Text File Line by Line
// Using BufferedReader class
// Importing required classes
import java.io.*;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
// Main class
public class GFG {
// Main driver method
public static void main(String[] args)
{
// Declaring a string and initializing it with
// path of file present on the system
String path = "C:\\Users\\HP\\Desktop\\gfg.txt";
// Try block to check for exceptions
try (BufferedReader br
= new BufferedReader(new FileReader(path))) {
// Declaring a new string
String str;
// It holds true till threre is content in file
while ((str = br.readLine()) != null) {
// Printing the file data
System.out.println(br);
}
}
// Catch block to handle the exceptions
catch (IOException e) {
// Display pop up message if exceptionn occurs
System.out.println(
"Error while reading a file.");
}
}
}
Output:
Geeks for Geeks.
A computer science portal.
Welcome to this portal.
Hello Geek !!!
Similar Reads
Java Program to Read a File to String There are multiple ways of writing and reading a text file. This is required while dealing with many applications. There are several ways to read a plain text file in Java e.g. you can use FileReader, BufferedReader or Scanner to read a text file. Given a text file, the task is to read the contents
8 min read
Java Program to Read Text From File From a Specified Index In a file system, without reading the previous text we cannot directly access the specific index. Thus, Reading text from a file from the specific index is achieved by skipping all the previous characters of a specified index. To read text from an index n, we need to skip (n-1) bytes. Here, we will
1 min read
Java program to read all mobile numbers present in given file In this article, we are going to discuss a Java program that reads all mobile numbers from a given text file and writes the correct mobile number to another output file. For this, we are going to use regular expression to identify the correct mobile number. Note: The number should meet the correct c
3 min read
Java program to read all Emails present in a Given file Given a file as input.txt containing some email ids which are mixed with other data. The task is to read this input file line by line and if any email id is found in that line, write that email id to another file, which is output.txt. Example: Input: input.txt Output: output.txt Approach: To detect
3 min read
Java Program to Convert File to a Byte Array Here, we will go through the different ways to convert file to byte array in Java. Note: Keep a check that prior doing anything first. Create a file on the system repository to deal with our program\writing a program as we will be accessing the same directory through our programs. Methods: Using rea
3 min read