Java:使用NIO和System.in

是否可以将NIO与System.in一起使用?

我想以某种方式将’stdin’视为可选择的频道。 有没有人找到办法做到这一点?

重复:

如何从InputStream获取SelectableChannel?

嗯……第二个想法,对于stdin可能有一个解决方案。 我找到了这个参考:

http://www.javafaq.nu/java-example-code-346.html

特别是:

SystemInPipe.java (将stdin封装为可选择的通道的类)

我不知道SelectableChannel ,但您可以将InputStream转换为ReadableByteChannel

 using java.nio.channels.Channels; ... ReadableByteChannel in = Channels.newChannel(System.in); 

您可以创建一个Pipe和一个自定义线程来读取标准输入并写入管道。

一旦你有了Pipe你就可以获得输入通道。