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));
}
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));
}
- csr for
-
JDK-8191490 Add Reader and Writer access to java.lang.Process
-
- Closed
-
- relates to
-
JDK-8265989 System property for the native character encoding name
-
- Resolved
-