如何在Java 8中逐行读取文件? [英] How to read file line by line in Java 8?

查看:130
本文介绍了如何在Java 8中逐行读取文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Java 8中,我看到在Files类中添加了名为lines()的新方法,可用于在Java中逐行读取文件。它适用于大文件吗?我的意思是我们可以加载前1000行然后第二组1000行。我有1GB的巨大文件,它会起作用吗?

In Java 8 I see new method is added called lines() in Files class which can be used to read a file line by line in Java. Does it work for huge files? I mean can we load first 1000 lines then second set of 1000 lines. I have huge file with 1GB, Will it work?

有人可以共享代码片段如何使用它吗?

Could someone share code snippet how to use it?

推荐答案


它对大文件有用吗? [...]我有1GB的巨大文件,是否可以
工作?

Does it work for huge files? [...] I have huge file with 1GB, Will it work?

据我所见应该适用于大文件(但我还没试过):

As far as I can see it should work well for big files as well (but I haven't tried):

try(Stream<String> lines = Files.lines(path)){
    lines.filter(...).map(...)....foreach(...);
}




我的意思是我们可以加载前1000行然后第二组1000行。

I mean can we load first 1000 lines then second set of 1000 lines.

一次读取多少行是Files.lines特有的实现(可能使用BufferedReader) ,但我可能错了。)

How many lines are read at one time is implementation specific to Files.lines (which probably uses a BufferedReader, but I might be wrong).

这篇关于如何在Java 8中逐行读取文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆