栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

使用Java重定向的stdin和stdout运行外部程序

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

使用Java重定向的stdin和stdout运行外部程序

如果必须使用

Process
,则类似这样的方法应该起作用:

public static void pipeStream(InputStream input, OutputStream output)   throws IOException{   byte buffer[] = new byte[1024];   int numRead = 0;   do   {      numRead = input.read(buffer);      output.write(buffer, 0, numRead);   } while (input.available() > 0);   output.flush();}public static void main(String[] argv){   FileInputStream fileIn = null;   FileOutputStream fileOut = null;   OutputStream procIn = null;   InputStream procOut = null;   try   {      fileIn = new FileInputStream("test.txt");      fileOut = new FileOutputStream("testOut.txt");      Process process = Runtime.getRuntime().exec ("/bin/cat");      procIn = process.getOutputStream();      procOut = process.getInputStream();      pipeStream(fileIn, procIn);      pipeStream(procOut, fileOut);   }   catch (IOException ioe)   {      System.out.println(ioe);   }}

注意:

  • 确保
    close
  • 更改为使用缓冲流,我认为原始
    Input/OutputStreams
    实现可能一次复制一个字节。
  • 该过程的处理方式可能会根据您的特定过程而有所不同:这
    cat
    是使用管道I / O的最简单示例。


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/437478.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号