How can I read a large text file line by line using Java?

To read a large text file line by line in Java, you can use a BufferedReader and pass it a FileReader object to read the file. Here's an example of how you can do this:

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;

public class Main {
  public static void main(String[] args) {
    // Set the file path
    String filePath = "C:\\folder\\file.txt";

    try {
      // Create a FileReader object
      FileReader fr = new FileReader(filePath);

      // Create a BufferedReader object
      BufferedReader br = new BufferedReader(fr);

      // Read the file line by line
      String line;
      while ((line = br.readLine()) != null) {
        // Process the line
        System.out.println(line);
      }

      // Close the reader
      br.close();
    } catch (IOException e) {
      e.printStackTrace();
    }
  }
}

This code will read the file line by line and print each line to the console. The BufferedReader class provides a convenient way to read a large file efficiently, as it reads the file in chunks and stores them in a buffer, rather than reading the file one character at a time.

Note that this example uses the FileReader class, which reads text from a file using the default encoding (usually UTF-8). If you need to use a different encoding, you can use the InputStreamReader class, which allows you to specify the encoding to be used.