How do i implement a function in java nio say nextLineNio() which reads next line from FileChannel incrementally as the file could be huge, my initial idea was to read into a ByteBuffer of size greater then maximum length of a line which could exist and read() from the FileChannel if required, but the problem which i see is how do i un-read the last sequence of bytes which do not end in a newline. Also how do i ensure that the nextLineNio() function doesn't fails as the nio api is asynchronous. Any help or already existing implementations?
Thanks
As @EJP points out, it is quite likely that you will get more than enough performance using BufferedReader.readLine() Unless you know this is not suitable, this is what I would suggest you keep things simple.
http://vanillajava.blogspot.com/2011/01/how-slow-can-you-readwrite-files-in.html
I would memory map the whole file and search for newlines to get a line at a time. This avoid the need to worry about buffer sizes or multiple reads (or the problem of reading too much)
NIO is synchronous by default, file IO is only synchronous (until Java 7). To make Sockets asynchronous you have to call a special setter.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With