开发者

Java: use NIO with System.in [duplicate]

开发者 https://www.devze.com 2022-12-14 08:27 出处:网络
This question already has answers here: 开发者_运维知识库How to get SelectableChannel from an InputStream?
This question already has answers here: 开发者_运维知识库 How to get SelectableChannel from an InputStream? (2 answers) Closed 6 years ago.

Is it possible to use NIO with System.in?

I would like to somehow treat 'stdin' as a selectable channel. Has anyone found a way to do this?


I don't know about a SelectableChannel, but you can convert an InputStream to a ReadableByteChannel with

using java.nio.channels.Channels;

...

ReadableByteChannel in = Channels.newChannel(System.in);


You could create a Pipe and a custom thread to read from standard input and write to the pipe.

Once you have the Pipe you can then get the input channel.


duplicate:

How to get SelectableChannel from an InputStream?

hmmm... on second thought, for stdin there may be a solution. I found this reference:

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

and in particular:

SystemInPipe.java (class which encapsulates stdin as a selectable channel)

0

精彩评论

暂无评论...
验证码 换一张
取 消