代码之家  ›  专栏  ›  技术社区  ›  StealthRT

输出流未转换为字符串变量

  •  0
  • StealthRT  · 技术社区  · 5 年前

    输出流 把它串成一个字符串,这样我就可以用我的逻辑更好地检查它。

    工作得很好 如果我只是想写 慰问 仅此而已。不过,我想把它储存起来 输出 一串 .

    public static void main(String[] args) throws IOException {
        Process p;
    
        try {
            p = Runtime.getRuntime().exec("cmd");
            new Thread(new SyncPipe(p.getErrorStream(), System.err)).start();
            new Thread(new SyncPipe(p.getInputStream(), System.out)).start();
            PrintWriter stdin = new PrintWriter(p.getOutputStream());
            stdin.println("cd C:/Local Apps/xxx/xxx/xxxx/eclipse");
            stdin.println("lscm login -r https://xxxx.xxxx.xxxx.xxxx:9443/ccm -n xxxx.xxxx.xxx.xxxx -u itsme -P gr^34dbtfgt7y");
            stdin.close();
    
            p.waitFor();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    
    }
    
    class SyncPipe implements Runnable {
        public SyncPipe(InputStream istrm, OutputStream ostrm) {
            istrm_ = istrm;
            ostrm_ = ostrm;
        }
    
        public void run() {
            try {
                final byte[] buffer = new byte[1024];
    
                for (int length = 0; (length = istrm_.read(buffer)) != -1;) {
                    ostrm_.write(buffer, 0, length);                    
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    
        private final OutputStream ostrm_;
        private final InputStream istrm_;
    }
    

    ostrm_.write(buffer, 0, length);
    

    我在谷歌上找到了各种各样的东西:

    String string = new String(ostrm_.toString());
    System.out.println(string);
    
    String string = new String(ostrm_.toString("UTF-8"));
    System.out.println(string);
    
    final PrintStream blah = new PrintStream(ostrm_);
    final String string = blah.toString();
    System.out.println(string);
    
    String blah = ostrm_.toString();
    System.out.println(string);
    
    byte[] blah = buffer;
    String string = blah.toString();
    

    输出流写入程序 但是,我又一次似乎无法让它工作。所以,如果一个Java大师能帮助我解决这个看起来很容易却很难解决的问题,那就太好了。。。。

    更新1 后备箱和发动机罩:

    enter image description here

    4 回复  |  直到 5 年前
        1
  •  1
  •   boot-and-bonnet    5 年前

    你可以用 readAllBytes() String 使用默认字符集:

    System.out.println(new String(istrm_.readAllBytes()));
    
        2
  •  0
  •   madhepurian    5 年前

    请试试这个:

    byte[] blah = buffer;
    String string = new String(blah);
    
        3
  •  0
  •   StealthRT    5 年前

    String line;
    BufferedReader reader = new BufferedReader(new InputStreamReader(istrm_));
    
    while ((line = reader.readLine()) != null) {
        System.out.println(line);
    }
    
        4
  •  0
  •   Andreas dfa    5 年前

    你的代码做事情的方式很难。要在特定工作目录中运行命令并将输出发送到控制台,请使用 ProcessBuilder inheritIO() .

    问题中的所有代码都可以简化为:

    public static void main(String[] args) throws IOException {
        new ProcessBuilder("cmd", "/c",
                           "lscm", "login",
                           "-r", "https://xxxx.xxxx.xxxx.xxxx:9443/ccm",
                           "-n", "xxxx.xxxx.xxx.xxxx",
                           "-u", "itsme",
                           "-P", "gr^34dbtfgt7y")
                .directory(new File("C:/Local Apps/xxx/xxx/xxxx/eclipse"))
                .inheritIO()
                .start()
                .waitFor();
    }
    

    要将命令的所有输出收集到一个字符串中,首先要通过调用 redirectErrorStream(true) ,然后将输出收集到 String 使用适当的字符集。

    Charset ,因为这可能是命令输出的内容。

    String[] command = {
            "cmd", "/c",
            "lscm", "login",
            "-r", "https://xxxx.xxxx.xxxx.xxxx:9443/ccm",
            "-n", "xxxx.xxxx.xxx.xxxx",
            "-u", "itsme",
            "-P", "gr^34dbtfgt7y" };
    Process p = new ProcessBuilder(command)
            .directory(new File("C:/Local Apps/xxx/xxx/xxxx/eclipse"))
            .redirectErrorStream(true) // stderr > stdout
            .start();
    p.getOutputStream().close(); // stdin < NUL
    String output;
    try (InputStream in = p.getInputStream()) {
        output = new String(in.readAllBytes());
    }
    int errorCode = p.waitFor();
    
    System.out.print(output);
    if (errorCode != 0)
        System.out.println("Program terminated with error code " + errorCode);
    

    上面的代码是针对Java9+的。对于Java 7+,获取 output

    try (InputStream in = p.getInputStream()) {
        ByteArrayOutputStream bytes = new ByteArrayOutputStream(1024);
        byte[] buf = new byte[1024];
        for (int len; (len = in.read(buf)) > 0; )
            bytes.write(buf, 0, len);
        output = bytes.toString();
    }