700字范文,内容丰富有趣,生活中的好帮手!
700字范文 > java 读取txt文件指定行_在Java中读取txt文件中的特定行

java 读取txt文件指定行_在Java中读取txt文件中的特定行

时间:2021-10-02 09:07:36

相关推荐

java 读取txt文件指定行_在Java中读取txt文件中的特定行

要获得第N行,您必须阅读它之前的所有行.

如果不止一次这样做,最有效的方法可能是首先将所有行加载到内存中.

private final List words = new ArrayList();

private final Random random = new Random();

public String randomWord() throws IOException {

if (words.isEmpty()) {

BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream("../image/ListeMot.txt")));

String line;

while ((line = br.readLine()) != null)

words.add(line);

br.close();

}

return words.get(random.nextInt(words.size()));

}

BTW:theWord的参数是什么意思?

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。