Decorator涉及模式
·在程序中用一个对象(the Decorators)包装另外的一个对象,就是一种被称为Decorator的设计模式。
·如果要设计自己的IO包装类,这个类需要继承以FilterXXX命名的类,例如,设计一对输入输出包装类:RecordInputStream和RecordOutputStream,来完成从数据库文件中读取记录和往数据库文件中写入记录。
·Exception类从Throwable类继承的三个printStackTrace方法的定义如下:
-public void printStackTrace()
-public void printStackTrace(PrintStream s)
-public void printStackTrace(PrintWriter s)
该如何把printStackTrace方法打出的详细异常信息存储到一个字符串中?
import java.io.*;
public class TestPrintWriter {
/**
* Method main
*
*
* @param args
*
*/
public static void main(String[] args) {
// TODO: Add your code here
try {
throw new Exception("test");
}
catch (Exception ex) {
StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw);
ex.printStackTrace(pw);
System.out.println (ex);
System.out.println ("======================================");
System.out.println(sw.toString());
}
}
}
执行结果:
java.lang.Exception: test
======================================
java.lang.Exception: test
at TestPrintWriter.main(TestPrintWriter.java:14)
Process completed.