java 流的用法

java中只有输出流才需要刷新缓冲区
PrintWriter和BufferedWriter都是继承java.io.Writer,所以很多功能都一样。不过PrintWriter提供println()方法可以写不同平台的换行符,而BufferedWriter可以任意设定缓冲大小。
OutputStream可以直接传给PrintWriter(BufferedWriter不能接收),如:
PrintWriter out
   = new PrintWriter(new BufferedOutputStream(new FileOutputStream("foo.out")));
或者用OutputStreamWriter来将OutputStream转化为Wrtier.这时就可以用BufferedWriter了。
API documents中都写的很清楚了。


 // 执行bash
  public static void execCommandByRuntime(String cmd)
  {
    BufferedInputStream ebis = null;
    BufferedInputStream bis = null;
    try {
      int bufSize = 4096;

      Runtime rt = Runtime.getRuntime();    
      Process p = rt.exec(cmd);
      byte[] buf = new byte[bufSize];
      int len;
      ebis = new BufferedInputStream(p.getErrorStream(), bufSize);
      while ((len = ebis.read(buf)) != -1) {
        System.out.println("ERROR CMD:"+cmd);
        System.out.write(buf, 0, len);
      }
      bis = new BufferedInputStream(p.getInputStream(), bufSize);
      while ((len = bis.read(buf)) != -1) {
        System.out.println("CMD:"+cmd);
        System.out.write(buf, 0, len);
      }
    } catch (Exception e) {
      e.printStackTrace();
    } finally {
      try {
        if (null != ebis) {
          ebis.close();
        }
        if (null != ebis) {
          bis.close();
        }
      } catch (IOException e) {
        e.printStackTrace();
      }
    }
  }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值