javafilenio

Non-blocking (NIO) reading of lines


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?


Solution

  • 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.

    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.