如何打印Process Builder的输出

时间:2015-02-16 06:12:37

标签: java jsp

我有使用流程构建器打印ipconfig命令输出的代码。但我想一次打印一行。我们的想法是从输出中删除几行,客户端不希望在输出中看到这一行。以下是代码。

{
File file=new File("D:\\LC");
String[] command = {"CMD", "/C", "dir"};
ProcessBuilder probuilder = new ProcessBuilder( command );
probuilder.directory(file);
Process p = pb.start();
BufferedReader reader = new BufferedReader(new InputStreamReader(p.getInputStream()));
String readline;
while ((readline = reader.readLine()) != null) 
{
    out.println(readline);
    out.println("<br>");
}
}

此处在While循环中,readline存储单个镜头中的整个值,因此我无法从输出中删除一些信息。我希望readline一次取值一行,这样通过使用循环和条件我可以删除我不想打印的行。 感谢您的支持。

1 个答案:

答案 0 :(得分:-1)

您的代码中存在拼写错误,应该是probuilder.start()而不是pb.start()

我已按预期运行程序,readLine()逐行读取。

 public static void main(String[] args) throws IOException {
    String[] cmd = { "CMD", "/C", "dir" };
    ProcessBuilder probuilder = new ProcessBuilder(cmd);
    Process p = probuilder.start();
    BufferedReader reader = new BufferedReader(new InputStreamReader(p.getInputStream()));
    String readline;
    int i = 0;
    while ((readline = reader.readLine()) != null) {
        System.out.println(++i + " " + readline);
    }
}

输出: -

1 Volume in drive C is System 2 Volume Serial Number is 1C25-498E 3 4 Directory of C:\juno\ksaravan\workspace\CommandPrompt 5 6 01/13/2015 12:47 PM <DIR> . 7 01/13/2015 12:47 PM <DIR> .. 8 02/13/2015 10:57 AM 623 .classpath 9 01/05/2015 12:48 PM 389 .project