Chinaunix首页 | 论坛 | 博客
  • 博客访问: 705424
  • 博文数量: 178
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 1507
  • 用 户 组: 普通用户
  • 注册时间: 2014-04-27 23:20
文章分类

全部博文(178)

文章存档

2015年(58)

2014年(121)

我的朋友

分类: Java

2015-01-10 22:54:31

import   java.io.*;   
    
  public   class   Ping   {   
          public   static   void   main(String[]   args)   {   
                  String   command="pig    ";   
                  StringWriter   sw   =   new   StringWriter();   
          try   {   
              Process   child=Runtime.getRuntime().exec(command);   
              child.waitFor();   
              System.out.println(child.exitValue());   
          }   catch   (   InterruptedException   ie   )   {     
                  System.out.println(   ie   );   
                  ie.printStackTrace(new   PrintWriter(sw));   
          }   catch   (IOException   ioe)   {   
                  System.out.println(   ioe   );   
                  ioe.printStackTrace(new   PrintWriter(sw));   
          }   
          System.out.println(sw);   
          }   
  }   
  sw.toString()就是你要的String

e.printStackTrace是定向到err的输出中,如果要e的信息直接e.toString就可以了

如果用e.toString,信息不全面,不能知道错误的具体行数。   
  StringWriter   sw=new   StringWriter();   
  ………   
  ………   
  e.printStackTrace(new   PrintWriter(sw,true));   
  String   str=sw.toString;   
  //str中就是详细的错误信息。  

 


本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/dancingtiger/archive/2008/05/09/2421075.aspx

阅读(2260) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~