gpt4 book ai didi

java - BufferedReader 未读取文本文件中的所有行

转载 作者:行者123 更新时间:2023-12-01 12:58:03 26 4
gpt4 key购买 nike

我有一个函数。

public ArrayList<String> readRules(String src) {
try (BufferedReader br = new BufferedReader(new FileReader(src))) {
String sCurrentLine;
while ((sCurrentLine = br.readLine()) != null) {
System.out.println(sCurrentLine);
lines.add(sCurrentLine);
}
} catch (IOException e) {
e.printStackTrace();
}
return lines;
}

我的文件有 26.400 行,但此函数仅读取文件末尾的 3400 行。如何读取文件中的所有行。谢谢!

最佳答案

为什么不使用实用方法Files.readAllLines()(自 Java 7 起可用)?

此方法可确保在读取所有字节或引发 IOException(或另一个运行时异常)时关闭文件。

使用指定的字符集将文件中的字节解码为字符。

public ArrayList<String> readRules(String src) {
return Files.readAllLines(src, Charset.defaultCharset());
}

关于java - BufferedReader 未读取文本文件中的所有行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23736429/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com