[英]Getting the output stream of “cmd.exe” in Java
I wanted to create something like a remote control for the command line in Windows. 我想为Windows中的命令行创建类似遥控器的内容。
For this I am using a scanner, but... 为此,我正在使用扫描仪,但是...
The problem is, when I read the whole line with nextLine() from the stream, the prompt will be missing (becouse is is printed, but not in a line) - and when I read the next word with next(), the line break is missing and you will lose the overview. 问题是,当我从流中使用nextLine()读取整行时,提示将丢失(因为已打印,但不在一行中)-当我使用next()读取下一个单词时,该行缺少中断,您将失去概览。 However, some information is even missing.
但是,某些信息甚至丢失。
package com;
import java.io.IOException;
import java.util.Scanner;
public class StdinCmd extends Thread {
public void run() {
try {
execute();
} catch (IOException e) {
e.printStackTrace();
}
}
public void execute() throws IOException {
Scanner reader = new Scanner(MainClient.getProcess().getInputStream()); // <- getting the stream
StdoutSocket stdoutSocket = new StdoutSocket();
while (true) {
while (reader.hasNext()) {
stdoutSocket.executeNext(reader.next()); // <- send it to the socket (the controller). This is what will be displayed at the end.
}
}
}
}
I attached a screenshot of how it should look like, and how it looks at the end: 我附上了它的外观以及最终外观的屏幕截图:
http://www.mediafire.com/?jma31ezg8ansfal http://www.mediafire.com/?jma31ezg8ansfal
I hope you can help me and I gave you enough information! 希望您能帮助我,我给您足够的信息!
Don't use Scanner
or BufferedReader
, but instead read directly from the InputStream
... 不要使用
Scanner
或BufferedReader
,而是直接从InputStream
读取...
InputStream is = null;
try {
is = MainClient.getProcess().getInputStream();
int in = -1;
while ((in = is.read()) != -1) {
System.out.print(((char)in));
}
} catch (IOException exp) {
exp.printStackTrace();
} finally {
try {
is.close();
} catch (Exception exp) {
}
}
Personally I really don't like scanner so much. 我个人真的不太喜欢扫描仪。 If you want to read input line's from a user and send it through a socket.. Who not then just use a BufferedReader with System.in?
如果您想从用户那里读取输入行并通过套接字发送它。那么谁又不只是将BufferedReader与System.in一起使用呢? Read a line and send it through the socket.
读一行并通过套接字发送。
BufferedReader br = new BUfferedReader(new InputStreamReader(System.in));
String line = null;
while((line = br.readLine()) != null){
OutSocket.send(line); // or how you send it..
}
~Foorack 〜Foorack
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.