当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Java Process getInputStream()用法及代码示例


Process类getInputStream()方法

  • getInputStream() 方法可在java.lang包。
  • getInputStream() 方法用于获取进程的输入流和sub-process。
  • getInputStream() 方法是一个非静态方法,它只能通过类对象访问,如果我们尝试使用类名访问方法,那么我们将得到一个错误。
  • getInputStream() 方法在返回输入流时不抛出异常。

用法:

    public abstract InputStream getInputStream();

参数:

  • 它不接受任何参数。

返回值:

这个方法的返回类型是InputStream,它返回与流程和子流程的标准输出相关联的输入流。

例:

// Java program to demonstrate the example 
// of InputStream getInputStream() method 
// of Process 

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

public class GetInputStream {
    public static void main(String[] args) throws Exception {
        // Instantiating Process object
        System.out.println("Process Instantiated");
        Process pr = Runtime.getRuntime().exec("notepad.exe");

        // By using getInputStream() method is to get the 
        // input stream of pr object
        InputStream is = pr.getInputStream();

        // Read from InputStream
        for (int k = 0; k < is.available(); ++k)
            System.out.println("Input stream = " + is.read());

        // Destroy a process
        pr.destroy();

        System.out.println("Process Destroyed");
    }
}

输出

Process Instantiated
Process Destroyed


相关用法


注:本文由纯净天空筛选整理自Preeti Jain大神的英文原创作品 Java Process getInputStream() method with example。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。