Since we are very familiar with this topic, we put more emphasis on finding the small differences between them. Here we have to read from a file in the local directory which has a text file, for example "gfg.txt". Make the contents of the file look like this:
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:
Method 1: Using Scanner class
Scanner is a class in the java.util package, used to obtain primitive types (such as int, double, etc.) and string input. This is the easiest way to read input in a Java program, but if you want an input method for time-constrained scenarios (such as competitive programming), it is not efficient. The Scanner class is used to read large files line by line. Scanner breaks its input into tokens, which match spaces by default.
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. In general, BufferedReader() is used to process large files. BufferedReader is very efficient to read.
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 !!!