Execute multiple commands by outputstream in Java

来源:互联网 发布:sql 字段默认值 编辑:程序博客网 时间:2024/06/07 07:20
public class MyProcess {public static void main(String[] args) {try {// bash, su or any other command invoke a shell interpreter// we execute the commands sent from outputstream by this// interpreterProcess process = Runtime.getRuntime().exec("bash\n");DataOutputStream outputStream = new DataOutputStream(process.getOutputStream());outputStream.writeBytes("echo nothing\n");outputStream.writeBytes("ls -al\n");outputStream.writeBytes("exit\n");outputStream.flush();outputStream.close();process.waitFor();BufferedReader rd = new BufferedReader(new InputStreamReader(process.getInputStream()));String string = null;while ((string = rd.readLine()) != null) {System.out.println(string);}} catch (Exception e) {// TODO: handle exception}}

0 0
原创粉丝点击