try {
   ...
   ...
}catch(Exception e) {
   e.printStackTrace();
}
请问如何将e.printStackTrace()显示的信息保存到一个指定的文档里?谢谢

解决方案 »

  1.   

    活活 用log4j 很简单的
      

  2.   

    用log4j,如果是WEB项目,在TOMCAT发布的话,会把控制台信息保存到日志文件的
      

  3.   

    谢谢两位。
    我现在是在APPLICATION里出抛出的,那怎么才使用log4j这个呢?
      

  4.   


            try {
                int i = 1/0;
            } catch (Exception e) {
                try {
                    new PrintWriter(new BufferedWriter(new FileWriter("c:/exception.txt",true)),true).println(e.toString());
                } catch (IOException e1) {
                    e1.printStackTrace();
                }
            }
      

  5.   

    还可以用重定向。PrintStream myStream=new PrintStream(new FileOutputStream("文件名"));
    System.setErr(myStream);     //重定向到myStream这样可以把所有的错误信息都输出到文件。
      

  6.   

    5楼的只会打印message,不会打印trace
    稍微改一下就好了
    try {
    int i = 1 / 0;
    } catch (final Exception e) {
    try {
    new PrintWriter(new BufferedWriter(new FileWriter(
    "c:/exception.txt", true)), true).println(new Object() {
    public String toString() {
    StringWriter stringWriter = new StringWriter();
    PrintWriter writer = new PrintWriter(stringWriter);
    e.printStackTrace(writer);
    StringBuffer buffer = stringWriter.getBuffer();
    return buffer.toString();
    }
    });
    } catch (IOException e1) {
    e1.printStackTrace();
    }
    }
      

  7.   

    顶!
    也可以这样 e.printStackTrace(new FileOutputStream("文件名"));