线路的非阻塞(NIO)读取 [英] Non-blocking (NIO) reading of lines
问题描述
我需要使用java.nio
逐行读取文件,但是nio
没有像readline()
那样的方法来一次读取一个完整的行.有什么解决方案?
I need to read a file line by line using java.nio
, but nio
doesn't have a method like readline()
to read one, complete line at a time. What solutions are there?
推荐答案
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.
这篇关于线路的非阻塞(NIO)读取的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!