Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Non-blocking (NIO) reading of lines

Tags:

java

file

nio

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?

like image 734
Aashwin Jain Avatar asked Jul 26 '12 07:07

Aashwin Jain


2 Answers

I understand you guys don't like limitations, but in case the one asking don't have access to IO package or not allowed to import it for some reason the top answer is not helpful...

Two ways to do it completely IO free:

  1. java.nio.file.Files.lines , Returns a stream of lines, which is part of .util package and not .io package like bufferedReader.

  2. java.nio.file.Files.readAllLines , Returns a collection of lines which is iterable. Proceed to use an iterator orfor each to extract a single line.

cheers

like image 200
Daniel Braun Avatar answered Sep 22 '22 10:09

Daniel Braun


Why? NIO doesn't support reading lines. You can read millions of lines a second with BufferedReader.readLine(). I suggest that is sufficient.

like image 39
user207421 Avatar answered Sep 23 '22 10:09

user207421