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
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.
链接地址: http://www.djcxy.com/p/64086.html上一篇: 文件在网络上时,java FileChannnel.transferTo()是否巧妙地工作?
下一篇: Java nio从文件读取行