JDK-8191441 : (Process) add Readers and Writer access to java.lang.Process streams
  • Type: Enhancement
  • Component: core-libs
  • Sub-Component: java.lang
  • Priority: P3
  • Status: Resolved
  • Resolution: Fixed
  • Submitted: 2017-11-16
  • Updated: 2025-01-29
  • Resolved: 2021-06-07
The Version table provides details related to the release that this issue/RFE will be addressed.

Unresolved : Release in which this issue/RFE will be addressed.
Resolved: Release in which this issue/RFE has been resolved.
Fixed : Release in which this issue/RFE has been fixed. The release containing this fix may be available for download as an Early Access Release or a General Availability Release.

To download the current JDK release, click here.
JDK 17
17 b26Fixed
Related Reports
CSR :  
Relates :  
Description
Java.lang.Process should provide Reader and Writer access to process standard and error output and input streams.
The Readers and Writer streams convert from and to the byte streams using a character set.

The Charset can be provided as an argument if known. 
If not supplied, the file descriptor of the corresponding byte stream, (stdout, stderr, or stdin) is consulted.
If the file descriptor is a terminal (natively istty()), the charset associated with the terminal via `java.io.Console` is used,
otherwise the charset is `Charset.defaultCharset()`.


Methods added to java.lang.Process:

     public BufferedReader inputReader() {...}
     public BufferedReader inputReader(Charset charset) {...}

     public BufferedReader errorReader() {...}
     public BufferedReader errorReader(Charset charset) {...}

     public PrintWriter outputWriter() {...}
     public PrintWriter outputWriter(Charset charset) {...}


Example Reading Lines:

    Process p = new ProcessBuilder("cat").start();
         // Write a line to the process
        try (PrintWriter writer = p.outputWriter()) {
            writer.println("Now is the time: " + LocalDateTime.now());
        }
        // Read back all the lines
        try (BufferedReader reader = p.inputReader()) {
            String line;
            while ((line = reader.readLine()) != null) {
                System.out.println(line);
            }
        }

Example Streaming Lines:

       Process p = new ProcessBuilder("cat", "x.tmp").start();
        // Read all the lines
        try (BufferedReader reader = p.inputReader()) {
           reader.lines().forEach(s -> System.out.println(s));
        }

Comments
Changeset: 81600dce Author: Roger Riggs <rriggs@openjdk.org> Date: 2021-06-07 17:41:09 +0000 URL: https://git.openjdk.java.net/jdk/commit/81600dce24903cbd3476830e302c9f182c85efb3
07-06-2021