Reading non-blocking files

Does java have a non-blocking API for reading files? If it werenโ€™t wise to build it in C ++ and call it from a java application via JNI?

+7
source share
2 answers

No, FileChannel not covered by SelectableChannel .

Perhaps because not all OSs support it.

Windows does, and theoretically you can write a C ++ library for Windows and call it through JNI, but it is very difficult to integrate it with java.nio .

I would prefer the workflow to copy the contents of the file into the pipe and do non-blocking reads at the other end of the pipe.

+6
source

My original answer is now incorrect since adding AsynchronousFileChannel in Java 7.

You still cannot select in a file, but now there are two methods for reading files asynchronously: one that accepts a callback and another that returns Future .

It may be cleaner to use the callback method (and send the event from the callback) than to use a dedicated thread to poll the channel.

+23
source

All Articles