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 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 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 !!!