Home » Java programming language

Java Process getInputStream() method with example

Process Class getInputStream() method: Here, we are going to learn about the getInputStream() method of Process Class with its syntax and example.
Submitted by Preeti Jain, on December 10, 2019

Process Class getInputStream() method

  • getInputStream() method is available in java.lang package.
  • getInputStream() method is used to get the input stream of the process and sub-process.
  • getInputStream() method is a non-static method, it is accessible with the class object only and if we try to access the method with the class name then we will get an error.
  • getInputStream() method does not throw an exception at the time of returning input stream.

Syntax:

    public abstract InputStream getInputStream();

Parameter(s):

  • It does not accept any parameter.

Return value:

The return type of this method is InputStream, it returns input stream associated to the standard output of the process and subprocess.

Example:

// 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");
    }
}

Output

Process Instantiated
Process Destroyed


Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.