捕获的异常到控制台的的信息转为String输出,在保存日志方面有用
/** * 将 e.printStackTrace() 到控制台的信息,保存为一个 String * @param e * @return */ public static String getStackTraceAsString(Throwable e){ //StringWriter将包含堆栈信息 StringWriter stringWriter = new StringWriter(); //必须将StringWriter封装成PrintWriter对象,以满足printStackTrace的要求 PrintWriter printWriter = new PrintWriter(stringWriter); //获取堆栈信息 e.printStackTrace(printWriter); //转换成String,并返回该String return stringWriter.getBuffer().toString(); }