用Java读取文本文件

发布于 2024-08-30 16:09:35 字数 92 浏览 2 评论 0原文

我有一个文本文件。我想将内容从一行检索到另一行。 例如,文件可能有 200K 行。我想读取第78行到第2735行的内容。由于文件可能很大,所以我不想将整个内容读入内存。

I have a text file. I would like to retrieve the content from one line to another line.
For example, the file may be 200K lines. I want to read the content from line 78 to line 2735. Since the file may be very large, I do not want to read the whole content into the memory.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

无所谓啦 2024-09-06 16:09:35

使用 BufferedReader.readLine() 并计算行数。您将仅保留缓冲区大小和当前行在内存中。

不,如果不读取到该点的整个文件,就不可能到达第 3412 行(除非您的行都有固定的大小)。

Use BufferedReader.readLine() and count the lines. You'll keep only the buffer size and the current line in memory.

And no, it's not possible to get to line 3412 without reading the whole file up to that point (unless your lines all have a fixed size).

似梦非梦 2024-09-06 16:09:35

这是一个可能的解决方案的开始:

public static List<String> linesFromTo(int from, int to, String fileName)
        throws FileNotFoundException, IllegalArgumentException {
    return linesFromTo(from, to, fileName, "UTF-8");
}

public static List<String> linesFromTo(int from, int to, String fileName, String charsetName)
        throws FileNotFoundException, IllegalArgumentException {

    if(from > to) {
        throw new IllegalArgumentException("'from' > 'to'");
    }
    if(from < 1 || to < 1) {
        throw new IllegalArgumentException("'from' or 'to' is negative");
    }

    List<String> lines = new ArrayList<String>();
    Scanner scan = new Scanner(new File(fileName), charsetName);
    int lineNumber = 0;

    while(scan.hasNextLine() && lineNumber < to) {
        lineNumber++;
        String line = scan.nextLine();
        if(lineNumber < from) continue;
        lines.add(line);
    }

    if(lineNumber != to) {
        throw new IllegalArgumentException(fileName+" does not have "+to+" lines");
    }

    return lines;
}

Here's a start of a possible solution:

public static List<String> linesFromTo(int from, int to, String fileName)
        throws FileNotFoundException, IllegalArgumentException {
    return linesFromTo(from, to, fileName, "UTF-8");
}

public static List<String> linesFromTo(int from, int to, String fileName, String charsetName)
        throws FileNotFoundException, IllegalArgumentException {

    if(from > to) {
        throw new IllegalArgumentException("'from' > 'to'");
    }
    if(from < 1 || to < 1) {
        throw new IllegalArgumentException("'from' or 'to' is negative");
    }

    List<String> lines = new ArrayList<String>();
    Scanner scan = new Scanner(new File(fileName), charsetName);
    int lineNumber = 0;

    while(scan.hasNextLine() && lineNumber < to) {
        lineNumber++;
        String line = scan.nextLine();
        if(lineNumber < from) continue;
        lines.add(line);
    }

    if(lineNumber != to) {
        throw new IllegalArgumentException(fileName+" does not have "+to+" lines");
    }

    return lines;
}
单挑你×的.吻 2024-09-06 16:09:35

只需先逐行阅读并计算行号,然后开始在您提到的行位置获取您需要的内容。

Just simply read line by line first and count the line numbers and start getting the contents you need at the line position you mentioned.

平生欢 2024-09-06 16:09:35

我建议使用 RandomAccessFile,此类使您能够跳转到文件中的特定位置。因此,如果您想读取文件的最后一行,则不必读取前面的所有行,只需跳到该行即可。

I would suggest using a RandomAccessFile, this class enables you to jump to a specific location in a file. So if you want to read the last line of the file you don't have to read all of the previous lines you can just jump to that line.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文