Java ProcessBuilder redirectErrorStream()方法与示例

Syntax:

句法:

    public boolean  redirectErrorStream ();
    public ProcessBuilder  redirectErrorStream (boolean re_err_stm);

ProcessBuilder类redirectErrorStream()方法 (ProcessBuilder Class redirectErrorStream() method)

  • redirectErrorStream() method is available in java.lang package.

    redirectErrorStream()方法在java.lang包中可用。

  • redirectErrorStream () method is used to check whether this process builder combines standard error and standard output.

    redirectErrorStream()方法用于检查此流程生成器是否结合了标准错误和标准输出。

  • redirectErrorStream (boolean re_err_stm) method is used to puts the redirectErrorStream property of this process builder.

    redirectErrorStream(boolean re_err_stm)方法用于放置此流程生成器的redirectErrorStream属性。

  • These methods don't throw an exception at the time of combining error and output stream.

    这些方法在合并错误和输出流时不会引发异常。

  • These are non-static methods, it is accessible with the class object only and, if we try to access these methods with the class name then we will get an error.

    这些是非静态方法,只能通过类对象访问,如果尝试使用类名称访问这些方法,则会收到错误消息。

Parameter(s):

参数:

  • In the first case, it does not accept any parameter.

    在第一种情况下,它不接受任何参数。

  • In the second case, Boolean re_err_stm - This parameter represents the new property.

    在第二种情况下, 布尔值re_err_stm-此参数表示新属性。

Return value:

返回值:

In the first case, the return type of the method is boolean – It returns true then error output will be combined with the standard output so that by using the getInputStream() method of Process class can be read both error and output otherwise it returns false.

在第一种情况下,该方法的返回类型为布尔值 –返回true,然后将错误输出与标准输出合并,以便通过使用Process类的getInputStream()方法可以读取错误并输出,否则返回false 。

In the second case, the return type of the method is ProcessBuilder, it returns this process builder.

在第二种情况下,方法的返回类型为ProcessBuilder ,它将返回此流程生成器。

Example:

例:

// Java program to demonstrate the example 
// of redirectErrorStream() method of ProcessBuilder class

import java.io.*;
import java.util.*;

public class RedirectErrorStream {
    public static void main(String[] args) throws Exception {
        // Creating an object of List
        List l = new LinkedList();

        // By using add() method to add elements
        l.add("TextPad.exe");
        l.add("notepad.exe");

        // Instantiating ProcessBuilder object
        ProcessBuilder pr_bu = new ProcessBuilder(l);

        // By using redirectErrorStream() method is 
        // to check whether this error stream is redirected
        // or not
        System.out.println("pr_bu.redirectErrorStream() =" + pr_bu.redirectErrorStream());

        // By using redirectErrorStream(boolean re_err_stm) method is not to 
        // redirect the error stream
        pr_bu.redirectErrorStream(false);
        System.out.println("pr_bu.redirectErrorStream(false) =" + pr_bu.redirectErrorStream());

        // By using redirectErrorStream(boolean re_err_stm) //method is to 
        // redirect the error stream
        pr_bu.redirectErrorStream(true);
        System.out.println("pr_bu.redirectErrorStream(true) =" + pr_bu.redirectErrorStream());
    }
}

Output

输出量

pr_bu.redirectErrorStream() =false
pr_bu.redirectErrorStream(false) =false
pr_bu.redirectErrorStream(true) =true


翻译自: https://www.includehelp.com/java/processbuilder-redirecterrorstream-method-with-example.aspx

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值