Java 获取";的输出流;cmd.exe“;在爪哇

Java 获取";的输出流;cmd.exe“;在爪哇,java,Java,我想为Windows中的命令行创建一个类似远程控制的东西 为此,我使用扫描仪,但 问题是,当我用nextLine()从流中读取整行时,提示将丢失(因为已打印,但不是在一行中)-当我用next()读取下一个单词时,换行符丢失,您将丢失概览。然而,有些信息甚至丢失了 package com; import java.io.IOException; import java.util.Scanner; public class StdinCmd extends Thread { public

我想为Windows中的命令行创建一个类似远程控制的东西

为此,我使用扫描仪,但

问题是,当我用nextLine()从流中读取整行时,提示将丢失(因为已打印,但不是在一行中)-当我用next()读取下一个单词时,换行符丢失,您将丢失概览。然而,有些信息甚至丢失了

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.
            }
        }
    }
}
package-com;
导入java.io.IOException;
导入java.util.Scanner;
公共类StdinCmd扩展了线程{
公开募捐{
试一试{
执行();
}捕获(IOE异常){
e、 printStackTrace();
}
}
public void execute()引发IOException{

Scanner reader=新扫描仪(MainClient.getProcess().getInputStream());//不要使用
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) {
    }
}

就我个人而言,我真的不太喜欢扫描器。如果你想从用户那里读取输入行并通过套接字发送。那么谁不在System.in中使用BufferedReader呢? 读一行并通过插座发送

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

可能与几乎相同,但我以前尝试过类似的方法,并记得它在没有bug的情况下工作。但无论如何,谢谢!当我键入>ftp并输入ftp服务器的密码时,客户端和服务器都会崩溃,因为它没有正确重定向到流,并且在连接时带有密码字段。知道吗?我当破坏这种性质的远程应用程序时,这将是一个问题,因为stdin/stdout不再匹配。至于一种解决方案,我从未见过有人提出过,但我没有那么努力地寻找。。。