关于Process中的getOutputStream

No_what_cant 2014-12-30 10:20:38
一直不明白 getOutputStream 到底有什么作用

Test test=new Test();
Runtime runtime = Runtime.getRuntime();
Process process=null;
try {
process=runtime.exec("java -version");
InputThread in=test.new InputThread(process);
ErrorInputThread error=test.new ErrorInputThread(process);
in.start();
error.start();
try {
OutputStream stream = process.getOutputStream();
stream.write("ipconfig".getBytes());
stream.flush();
stream.close();
int waitFor = process.waitFor();
process.destroy();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

如果OuputStream是父进程向子进程的输入流的话 那么应该会先执行java -version 然后是ipconfig 命令

如果是子进程的输入流的话 那么我执行
Test test=new Test();
// TODO Auto-generated method stub
Runtime runtime = Runtime.getRuntime();
Process process=null;
try {
process=runtime.exec("notepad.exe");
InputThread in=test.new InputThread(process);
ErrorInputThread error=test.new ErrorInputThread(process);
in.start();
error.start();
try {
OutputStream stream = process.getOutputStream();
stream.write("ipconfig".getBytes());
stream.flush();
stream.close();
int waitFor = process.waitFor();
process.destroy();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

应该文本中有内容

getOutputStream到底有什么作用?
...全文
371 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
guest6379 2014-12-30
  • 打赏
  • 举报
回复
1. getInputStream()可以从新创建的process的stdout读取东西,java可以获取process的标准输出的东西。 2. getOutputStream()可以从java发送东西到新创建process的stdin,process可以从标准输入读这个东西,自己接着处理。 第一种情况用的很多,比如ifconfig,然后java读取ip进来处理。 第二种情况比较少用(我几乎没用过),我写了个例子,帮助理解: a.py 会从标准输入接收一个信息

import sys
print "begin python script"
# msg = sys.stdin.readline().strip('\n')
msg = raw_input()
print "stdin from java: {}".format(msg)
print 'end python script'
Test.java - 运行a.py,并发送一个信息到a.py的标准输入,最后从a.py的标准输出读取执行结果

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStream;

public class Test {
    public static void main(String[] argv) throws Exception {
        String command = "python a.py";
        Process child = Runtime.getRuntime().exec(command);
        OutputStream stdin = child.getOutputStream();
        stdin.write("I am Java\n".getBytes());
        stdin.flush();
        child.waitFor();

        System.out.println("begin to get output from python");

        String line = null;
        BufferedReader reader = new BufferedReader(new InputStreamReader(
                child.getInputStream()));
        while ((line = reader.readLine()) != null) {
            System.out.println("[stdout] " + line);
        }

        reader.close();
        stdin.close();
    }
}
结果:
begin to get output
[Stdout] begin python script
[Stdout] stdin from java: I am Java
[Stdout] end python script
rumlee 2014-12-30
  • 打赏
  • 举报
回复
你的这两个测试例子都没法接收输入啊?所以你当然没有测试效果。
android 串口驱动源代码 package android.serialport; import java.io.File; import java.io.FileDescriptor; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import android.util.Log; public class SerialPort { private static final String TAG = "SerialPort"; /* * Do not remove or rename the field mFd: it is used by native method close(); */ private FileDescriptor mFd; private FileInputStream mFileInputStream; private FileOutputStream mFileOutputStream; public SerialPort(File device, int baudrate) throws SecurityException, IOException { /* Check access permission */ if (!device.canRead() || !device.canWrite()) { try { /* Missing read/write permission, trying to chmod the file */ Process su; su = Runtime.getRuntime().exec("/system/bin/su"); /*String cmd = "chmod 777 " + device.getAbsolutePath() + "\n" + "exit\n";*/ String cmd = "chmod 777 /dev/s3c_serial0" + "\n" + "exit\n"; su.getOutputStream().write(cmd.getBytes()); if ((su.waitFor() != 0) || !device.canRead() || !device.canWrite()) { throw new SecurityException(); } } catch (Exception e) { e.printStackTrace(); throw new SecurityException(); } } mFd = open(device.getAbsolutePath(), baudrate); if (mFd == null) { Log.e(TAG, "native open returns null"); throw new IOException(); } mFileInputStream = new FileInputStream(mFd); mFileOutputStream = new FileOutputStream(mFd); } // Getters and setters public InputStream getInputStream() { return mFileInputStream; } public OutputStream getOutputStream() { return mFileOutputStream; } // JNI private native static FileDescriptor open(String path, int baudrate); public native void close(); static { System.loadLibrary("serial_port"); } }

62,616

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧