Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

java nio reading lines from a file

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

like image 533
FUD Avatar asked Nov 04 '22 05:11

FUD


1 Answers

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.

like image 113
Peter Lawrey Avatar answered Nov 07 '22 22:11

Peter Lawrey