Posted in

Java 文件中读取最后 N 行_AI阅读总结 — 包阅AI

包阅导读总结

1. 关键词:Java、文件读取、最后 N 行、BufferedReader、Scanner

2. 总结:

本文介绍了在 Java 中读取文件最后 N 行的多种方法,包括使用 BufferedReader、Scanner、NIO2 Files 和 Apache Commons IO,并对比了它们的代码复杂度、内存效率和性能,指出应根据具体需求选择合适的方法。

3. 主要内容:

– 读取文件最后 N 行是 Java 中的常见任务

– 介绍了多种实现方法

– 使用 BufferedReader

– 示例代码,通过链表存储,边读边处理,保持最后 N 行

– 使用 Scanner

– 类似 BufferedReader,逐行读取处理

– 使用 NIO2 Files

– 先读所有行到列表,再取最后 N 行,内存效率低

– 使用 Apache Commons IO

– 反向读取,高效适合大文件

– 结论

– 不同方法各有特点,根据需求选择

– 对比了各方法的代码复杂度、内存效率和性能

思维导图:

文章地址:https://www.javacodegeeks.com/read-last-n-lines-from-file-in-java.html

文章来源:javacodegeeks.com

作者:Yatin Batra

发布时间:2024/8/13 11:35

语言:英文

总字数:1009字

预计阅读时间:5分钟

评分:73分

标签:Java,文件处理,BufferedReader,Scanner,NIO2 Files


以下为原文内容

本内容来源于用户推荐转载,旨在分享知识与观点,如有侵权请联系删除 联系邮箱 media@ilingban.com

Reading the last N lines of a file is a common task in Java, especially when dealing with log files or other large files where only the most recent entries are of interest. Java provides multiple ways to accomplish this task, ranging from standard libraries to third-party utilities. Let us delve to explore four methods to read the last N lines from a file in Java using BufferedReader, Scanner, NIO2 Files, and Apache Commons IO.

1. Using BufferedReader

The BufferedReader class in Java can be used to efficiently read text from an input stream. Here’s how you can use it to read the last N lines from a file.

1.1 Code Example and Output

import java.io.BufferedReader;import java.io.FileReader;import java.util.LinkedList;import java.util.List;public class ReadLastNLinesUsingBufferedReader {    public static List<String> readLastNLines(String filePath, int n) throws Exception {        LinkedList<String> lines = new LinkedList<>();        try (BufferedReader reader = new BufferedReader(new FileReader(filePath))) {            String line;            while ((line = reader.readLine()) != null) {                lines.add(line);                if (lines.size() > n) {                    lines.removeFirst();                }            }        }        return lines;    }    public static void main(String[] args) throws Exception {        List<String> lastNLines = readLastNLines("example.txt", 5);        for (String line : lastNLines) {            System.out.println(line);        }    }}

The code reads a file line by line using BufferedReader. We use a LinkedList<String> to store the last N lines. As we read each line, we add it to the list, and if the list exceeds N lines, we remove the first line. This way, the list always contains only the last N lines.

If we want the last 5 lines, the output will be:

Line 4Line 5Line 6Line 7Line 8

2. Using Scanner

The Scanner class in Java is often used to parse primitive types and strings using regular expressions. It can also be used to read the last N lines of a file.

2.1 Code Example and Output

import java.io.File;import java.util.LinkedList;import java.util.List;import java.util.Scanner;public class ReadLastNLinesUsingScanner {    public static List<String> readLastNLines(String filePath, int n) throws Exception {        LinkedList<String> lines = new LinkedList<>();        try (Scanner scanner = new Scanner(new File(filePath))) {            while (scanner.hasNextLine()) {                lines.add(scanner.nextLine());                if (lines.size() > n) {                    lines.removeFirst();                }            }        }        return lines;    }    public static void main(String[] args) throws Exception {        List<String> lastNLines = readLastNLines("example.txt", 5);        for (String line : lastNLines) {            System.out.println(line);        }    }}

This approach is similar to the BufferedReader method. The Scanner reads the file line by line, and we store the last N lines in a LinkedList<String>. When the size of the list exceeds N, we remove the first element, ensuring that only the last N lines are retained.

If we want the last 5 lines, the output will be:

Line 4Line 5Line 6Line 7Line 8

3. Using NIO2 Files

The NIO2 API, introduced in Java 7, offers the Files class, which provides a lot of utility methods for file operations.

3.1 Code Example and Output

import java.nio.file.Files;import java.nio.file.Path;import java.nio.file.Paths;import java.util.List;public class ReadLastNLinesUsingNIO2Files {    public static List<String> readLastNLines(String filePath, int n) throws Exception {        Path path = Paths.get(filePath);        List<String> allLines = Files.readAllLines(path);        return allLines.subList(Math.max(allLines.size() - n, 0), allLines.size());    }    public static void main(String[] args) throws Exception {        List<String> lastNLines = readLastNLines("example.txt", 5);        for (String line : lastNLines) {            System.out.println(line);        }    }}

The Files.readAllLines() method reads all lines from a file into a list. We then use subList() to get the last N lines. This approach may be less memory efficient for large files since it reads all lines into memory at once.

If we want the last 5 lines, the output will be:

Line 4Line 5Line 6Line 7Line 8

4. Using Apache Commons IO

The Apache Commons IO library provides utility methods for file operations. The FileUtils class makes it easy to read the last N lines from a file.

4.1 Code Example and Output

import org.apache.commons.io.input.ReversedLinesFileReader;import java.io.File;import java.nio.charset.StandardCharsets;import java.util.ArrayList;import java.util.List;public class ReadLastNLinesUsingApacheCommonsIO {    public static List<String> readLastNLines(String filePath, int n) throws Exception {        List<String> result = new ArrayList<>();        try (ReversedLinesFileReader reader = new ReversedLinesFileReader(new File(filePath), StandardCharsets.UTF_8)) {            for (int i = 0; i < n; i++) {                String line = reader.readLine();                if (line == null) break;                result.add(0, line);            }        }        return result;    }    public static void main(String[] args) throws Exception {        List<String> lastNLines = readLastNLines("example.txt", 5);        for (String line : lastNLines) {            System.out.println(line);        }    }}

The ReversedLinesFileReader reads lines from a file in reverse order, which is ideal for fetching the last N lines. We use a loop to read the last N lines and add them to a list. The result is then returned in the correct order.

If we want the last 5 lines, the output will be:

Line 4Line 5Line 6Line 7Line 8

5. Conclusion

Reading the last N lines of a file in Java can be done in several ways, depending on the specific use case and the size of the file. The BufferedReader and Scanner methods are straightforward and work well for most cases. The NIO2 Files method is useful but may not be memory efficient for large files. The Apache Commons IO method is very efficient and easy to implement, especially for large files. Choose the approach that best fits your needs.

5.1 Comparison of Methods to Read Last N Lines From File in Java

Method Code Complexity Memory Efficiency Performance Code Example
BufferedReader Moderate High (memory efficient for moderate sizes) Good (linear time complexity) BufferedReader reads line by line and maintains a queue of last N lines.
Scanner Moderate High (similar to BufferedReader) Good (similar to BufferedReader) Scanner also reads line by line and maintains a queue of last N lines.
NIO2 Files Simple Low (reads all lines into memory) Potentially slow for large files (linear time but reads entire file) Files.readAllLines reads the entire file into memory, then extracts the last N lines.
Apache Commons IO Simple High (efficient for large files) Excellent (reads lines in reverse order) ReversedLinesFileReader reads lines from the end of the file, which is efficient for large files.