Java IO 系列----流和Decorator设计模式

    技术2022-05-20  59

    ·在程序中用一个对象(the Decorators)包装另外的一个对象,这是一种被称为Decorator的设计模式。

    ·如果要设计自己的IO包装类,这个类需要继承以FilterXXX命名的类,例如,设计一对输入输出包装类:RecordInputStream和RecordOutputStream,来完成从数据库文件中读取记录和往数据库文件中写入记录。

    ·编程实例:Exception类从Throwable类继承的三个printStackTrace方法的定义如下:

        --printStackTrace()     --printStackTrace(PrintStream s)

      --printStackTrace(PrintWriter s)   该如何把printStackTrace方法打出的详细异常信息存储到一个字符串中?

     

      import java.io.*; public class Iotest { /** * @param args */ public static void main(String[] args) { StringWriter sw = new StringWriter(); PrintWriter pw = new PrintWriter(sw); try { throw new Exception("test"); }catch(Exception e){ e.printStackTrace(pw); System.out.println(sw.toString()); //System.out.println(e.getMessage()); //这个也能返回异常信息,但不是详细信息,打印的结果是 test } } }

     

     

     

     

     


    最新回复(0)