📜  逐行读取大型文本文件的Java程序

📅  最后修改于: 2022-05-13 01:55:36.209000             🧑  作者: Mango

逐行读取大型文本文件的Java程序

由于我们对这个主题很熟悉,因此让我们施加更多压力以找出它们之间的细微差别。在这里,我们应该从本地目录中的一个文件中读取一个文本文件,比如“gfg.txt”。让文件里面的内容如下图所示:

Geeks for Geeks.
A computer science portal.
Welcome to this portal.
Hello Geek !!!

方法:

  1. 使用 Scanner 类
  2. 使用 BufferedReader 类

方法一:使用 Scanner 类

Scanner 是Java.util 包中的一个类,用于获取原始类型(如 int、double 等)和字符串。这是在Java程序中读取输入的最简单方法,但如果您想要一种输入法用于时间受限的场景(例如竞争性编程),则效率不高。 Scanner 类用于逐行读取大文件。 Scanner 将其输入分解为标记,默认情况下与空格匹配。



例子

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());
            }
        }
    }
}


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.");
        }
    }
}


输出:

Geeks for Geeks.
A computer science portal.
Welcome to this portal.
Hello Geek !!!

方法二:使用BufferedReader类

BufferedReader 用于逐行读取文件。基本上 BufferedReader() 用于处理大文件。 BufferedReader 对于阅读非常有效。

句法:

BufferedReader in = new BufferedReader(Reader in, int size);

例子:

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.");
        }
    }
}

输出:

Geeks for Geeks.
A computer science portal.
Welcome to this portal.
Hello Geek !!!