Although, I got answer for my question from Alexander, I'd like to share the most reactive ways that I found to read a file line by line. Both solutions use AsynchronousFileChannel
under the hood which is not always non-blocking but still okay to use in a reactive environment since it uses a dedicated thread pool for IO work.
Using utils from Spring Framework (ideal in WebFlux application)
import org.springframework.core.codec.StringDecoder;
import org.springframework.core.io.buffer.DataBufferUtils;
import org.springframework.core.io.buffer.DefaultDataBufferFactory;
import java.nio.channels.AsynchronousFileChannel;
import java.nio.file.Path;
import java.nio.file.StandardOpenOption;
public class AsyncFileRead {
public Flux<String> lines() {
StringDecoder stringDecoder = StringDecoder.textPlainOnly();
return DataBufferUtils.readAsynchronousFileChannel(() -> AsynchronousFileChannel.open(Path.of("test/sample.txt"),
StandardOpenOption.READ), DefaultDataBufferFactory.sharedInstance, 4096)
.transform(dataBufferFlux -> stringDecoder.decode(dataBufferFlux, null, null, null));
}
}
Using RxIo library
import org.javaync.io.AsyncFiles;
import reactor.core.publisher.Flux;
import java.nio.file.Path;
public class AsyncFileRead {
public Flux<String> lines() {
return Flux.from(AsyncFiles.lines(Path.of("test/sample.txt")));
}
}