是否可以将 NIO 与 System.in 一起使用?
我想以某种方式将“标准输入”视为可选频道。有没有人找到办法做到这一点?
我不知道 a SelectableChannel
,但是您可以将 a 转换InputStream
为ReadableByteChannel
with
using java.nio.channels.Channels;
...
ReadableByteChannel in = Channels.newChannel(System.in);
您可以创建一个Pipe
自定义线程来读取标准输入并写入管道。
一旦你有了,Pipe
你就可以得到输入通道。
复制:
如何从 InputStream 中获取 SelectableChannel?
嗯......再想一想,对于标准输入,可能有一个解决方案。我找到了这个参考:
http://www.javafaq.nu/java-example-code-346.html
特别是:
SystemInPipe.java(将标准输入封装为可选通道的类)