Read line using Java New I/O

前端 未结 3 1685
执念已碎
执念已碎 2021-01-21 07:49

What\'s the best way to read a line from a file using the New I/O ?

I can only get a byte at a time.

Any idea?

3条回答
  •  攒了一身酷
    2021-01-21 08:24

    Are you referring to the NIO introduced in Java 5.0, 7 years ago? Or the Asynchronous NIO adding in Java 7?

    In short the simple answer is that using BufferedReader is much, much simpler and not much slower.

    If you must use ByteBuffer, or you want that last bit of performance, you have to read one byte at a time. (And you have handle the situation that only part of the line has been read so you can run out of data in the ByteBuffer before you reach the new line)

提交回复
热议问题