java nio逐行读取 [英] java nio read line by line
问题描述
我需要使用java nio逐行读取文件
I need to read a file line by line using java nio
必须读取第一行并且必须进行一些处理
the first line has to be read and some processing has to happen
但是nio没有类似readline()的方法来读取一行
but nio does not have a method like readline() to read a line at one
请提供示例代码
推荐答案
NIO通常用于执行直接内存访问或块介导的批量数据传输。它确实做了其他事情,但其他功能更多地与阻塞和非阻塞数据访问有关。
NIO is typically used to do either direct memory access or block mediated bulk data transfers. It does do other things, but other features have more to do with blocking and non-blocking data access.
因此,您可能希望使用NIO来获取数据快速(或以非阻塞的方式);但是,如果你想逐行阅读,那么在NIO读入可用数据后进行线路检测会更好。这可以通过在NIO刚读取的缓冲区上放置行读取外观来轻松实现。
As such, you might want to use NIO to grab the data quickly (or in a non-blocking manner); however, if you want to "read line by line" you would be better served by doing the line detection after NIO has read in the available data. This could easily be implemented by putting a "line reading" facade over the buffer that NIO just read.
这篇关于java nio逐行读取的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!