在Java中从子进程获取和存储输出 [英] Fetch and store output from a subprocess in Java

查看:178
本文介绍了在Java中从子进程获取和存储输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在做一些需要我开始进行子进程(命令提示符)并在其上执行一些命令的东西。我需要从子进程获取输出并将其存储在文件或String中。
这是我到目前为止所做的,它不起作用:

I'm working on something that requires me to start to subprocess(command prompt) and execute some commands on it. I need to fetch the output from the subprocess and store it in a file or String. here's what I have done so far, and it doesn't work:

public static void main(String args[])
{
        try
    {
        Runtime RT = Runtime.getRuntime();
        String command = "cmd /c start javap java.lang.String"; 
        File file = new File("write.txt");
        Writer output = new BufferedWriter(new FileWriter(file));
            BufferedReader br = new(BufferedReader(newInputStreamReader(RT.exec(command).getInputStream()));
        String temp = br.readLine();
        while(!temp.equals(null))
        {
            output.write(temp);
            temp = br.readLine();
        }
        output.close();
        RT.exec("exit");
    }
    catch(Exception e)
    {
        System.out.println(e);
    }
}


推荐答案

开始更改:

Start changing this:

new(BufferedReader(newInputStreamReader(

收件人:

new BufferedReader(new InputStreamReader(

编译并查看是否仍有问题

Compile and see if you still have the problem

编辑

此外,有一个很好的理由为什么你不应该捕获异常,你也会捕获编程错误,如 NullPointerEx ception

Also, there is a good reason why you shouldn't catch Exception, you also catch programming errors like a NullPointerException

 while( !temp.equals(null)) { //Throws NullPointerExceptin when temp is null

更改为:

 while( temp != null ) { //!temp.equals(null)) {

最后你不必退出,因为你真的不在cmd里面。

Finally you don't have to "exit" since you're not inside the cmd really.

更正后的版本

此版本按您的意图运行:

This version runs as you intend:

import java.io.*;
class Rt { 
  public static void main(String args[]) throws Exception {
          Runtime RT = Runtime.getRuntime();
          String  command =  "javap java.lang.String" ; 
          File file = new File("write.txt");
          Writer output = new BufferedWriter(new FileWriter(file));
          BufferedReader br = new BufferedReader(new InputStreamReader(RT.exec(command).getInputStream()));
          String temp = br.readLine();
          while( temp != null ) { //!temp.equals(null)) {
              output.write(temp);
              temp = br.readLine();
          }
          output.close();
          //RT.exec("exit");
  }
}

编辑
最后的评论:

edit Final remarks:

从Java 1.5开始,调用命令的首选方法是使用 ProcessBuilder ,如果使用字符串数组而不是单个字符串(或varargs),则会更好。

Since Java 1.5 the preferred way to invoke a command is using ProcessBuilder and it is better if you use an array of strings instead of a single string ( or varargs ).

当您构建输出时,您可以删除文件对象并将文件名直接传递给文件编写者。

When you're building your output you can get rid of the file object and pass the file name directly to the filewriter.

在阅读该行时,您可以在条件中分配和评估。

While reading the line you can assign and evaluate in the condition.

Java的编码约定建议使用相同的左大括号。

Java's coding conventions suggest to use the opening brace in the same like.

这将是我的版本你的代码:

This would be my version of your code:

class Rt { 
   public static void main(String args[]) throws Exception {

      Writer output     = new BufferedWriter(new FileWriter ( "write.txt"));
      InputStream in    = new ProcessBuilder("javap", "java.lang.String").start().getInputStream();
      BufferedReader br = new BufferedReader( new InputStreamReader(in)); 
      String line = null;
      while( ( line = br.readLine() )   != null ) {
          output.write( line );
      }
      output.close();
  }
}

它可能还需要一些工作,但我希望它帮助你。

It might need still some work, but I hope it helps you.

这篇关于在Java中从子进程获取和存储输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆