Java: how to read and write to & from a process through a pipe (stdin / stdout)

(I'm new to java) I need to start the process and get 2 or 3 descriptors: for STDIN, STDOUT (and STDERR), so I can write the input to the process and get its output in the same way as command line lines behave (for example, grep ) / p>

in Python, this is achieved with the following code:

from subprocess import Popen, PIPE
p = Popen(cmd, shell=True, stdin=PIPE, stdout=PIPE)
(child_stdin, child_stdout) = (p.stdin, p.stdout)
child_stdin.write('Yoram Opposum\n')
child_stdin.flush()
child_stdout.readlines()

What is the Java equivalent?

I tried so far

Process p = Runtime.getRuntime().exec(cmd);
BufferedReader inp = new BufferedReader( new InputStreamReader(p.getInputStream()) );
BufferedWriter out = new BufferedWriter( new OutputStreamWriter(p.getOutputStream()) );
out.write( "Some Text!\n\n" );
out.flush();
line = inp.readLine();
print("response1: " + line );   // that ok
out.write( "Second Line...\n" );
out.flush();
line = inp.readLine();
print("response2: " + line );    // returns an empty string, if it returns,,,
inp.close();
out.close();

By the way, the first attempt only works with \ n \ n, but does not work with one \ n (why?)

The following code works, but all the data is entered in advance, and not the behavior I'm looking for:

out.write( "Aaaaa\nBbbbbb\nCcccc\n" );
out.flush();
line = inp.readLine();
print("response1: " + line );
line = inp.readLine();
print("response2: " + line );
line = inp.readLine();
print("response3: " + line );
line = inp.readLine();
print("response4: " + line );

output:

response1: AAAAA
response2: 
response3: bbbbbb
response4: 

the process being performed is as follows:

s = sys.stdin.readline()
print s.upper()
s = sys.stdin.readline()
print s.lower()
+3
source share
2 answers

ok, python, @Jon, EXTRA (0xA0, , Windows).

() 0xA0 , Java, python "" \n Java , .

, Java-:

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

public class Main {

    public static BufferedReader inp;
    public static BufferedWriter out;

    public static void print(String s) {
    System.out.println(s);
    }

    public static String pipe(String msg) {
    String ret;

    try {
        out.write( msg + "\n" );
        out.flush();
        ret = inp.readLine();
        return ret;
    }
    catch (Exception err) {

    }
    return "";
    }



    public static void main(String[] args) {

    String s;
    String cmd = "c:\\programs\\python\\python.exe d:\\a.py";

    try {

        print(cmd);
        print(System.getProperty("user.dir"));
        Process p = Runtime.getRuntime().exec(cmd);

        inp = new BufferedReader( new InputStreamReader(p.getInputStream()) );
        out = new BufferedWriter( new OutputStreamWriter(p.getOutputStream()) );

        print( pipe("AAAaaa") );
        print( pipe("RoteM") );

        pipe("quit")
        inp.close();
        out.close();
    }

    catch (Exception err) {
        err.printStackTrace();
    }
    }
}

python

import sys
s = sys.stdin.readline().strip()
while s not in ['break', 'quit']:
    sys.stdout.write(s.upper() + '\n')
    sys.stdout.flush()
    s = sys.stdin.readline().strip()
+8

, , :

s = sys.stdin.readline()
print s.upper()
s = sys.stdin.readline()
print s.lower()

, readline , s . , ... Java , , , .

, , - print , . , , - :

s = sys.stdin.readline()
println s.upper()
s = sys.stdin.readline()
println s.lower()

EDIT: ... , , , , , .

+2

Source: https://habr.com/ru/post/1773367/


All Articles