[原]Java流使用的一点总结

最经工作中碰到不少Java流的使用,总结如下:

1. 生成Zip格式,遇到的是要在一个Servlet中生成Zip文件,输出到web 客户端,直接下载。
   

    //
    ZipOutputStream 继承自 java.io.FilterOutputStream. 因此真正的写操作是通过参数OutputStream out去写的。
    其 void write(byte[] b, int off, int len) 最终调用了 out.write(b, off, len);

    如果要生成一个zip文件,构造时就这样写 new ZipOutputStream(new FileOutputStream(path));

2. 类似的写XML.
    XMLWriter writer = new XMLWriter(new FileOutputStream(path), formater)
    writer.write(doc).道理和上面类似
3. 写文本文件,追加。
    PrintStream ps = new PrintStream(new FileOutputStream(path, true), "utf-8")
    ps.println(s); // 能写boolean、int等各种类型。

    PrintSteam同样继承自FilterOutputStream
    其内部使用一个OutputStreamWriter的对象textOut来写。例如write(String s)最终调用到textOut.write(s);
   
    这里涉及到编码的问题。其参数里的"utf-8"最终传递到OutputStream。
   
    OutputStreamWriter的描述如下:
    An OutputStreamWriter is a bridge from character streams to byte streams: Characters written to it are encoded into bytes using a specified charset. The charset that it uses may be specified by name or may be given explicitly, or the platform's default charset may be accepted.
    OutputStream是一个字符流和字节流之间的桥梁。
    因此其提供了write(char[] cbuf, int off, int len) 和 write(String str, int off, int len) 用来写字符和字符串。
    OutputStream内部又通过一个StreamEncoder对象来序列化字符和字符串。
4. 写出到socket。
    DataOutputStream out = new DataOutputStream(socket.getOutputStream());
    out.writeBytes(bt);
    out.writeBoolean(boolean v) ;
    //....

    DataOutputStream同样是一个自FilterOutputStream.

5. 从文本中读取
    BufferedReader reader = new BufferedReader(new FileReader(path));
    reader.readLine();

    BufferedReader的模式和上面的Filter模式一样,其内部存储一个Reader对象为参数传进来并用来实际读取的对象。
    BufferedReader对应java 1.0的类就是BufferedInputStream,是一个FilterInputStream。

6. 从Socket中读取
    BufferedInputStream is = new BufferedInputStream(socket.getInputStream());
    is.read(bt, 0, bt.length());


总结:
    基类Stream系列是InputStream和OutputStream,他们是抽象类,要求的方法只有(以Output为例)
        void write(int b) throws IOException;
        void write(byte b[]) throws IOException
        void write(byte b[], int off, int len)
    其最基本的只是字节操作。第1个方法看似写一个整数,其实只写一个字节(最低八个bit)Writes the specified byte to this output stream. The general contract for write is that one byte is written to the output stream. The byte to be written is the eight low-order bits of the argument b. The 24 high-order bits of b are ignored.

    其子类分两个系列,一个是直接操作输出设备的,我们上面碰到的有文件(FileOutputStream)和Servlet输出(ServletOutputStream)。其他常用的还有一个 ByteArrayOutputStream,是直接在内存里操作的。
    再就是FilterOutputStream系列了,都是接收一个OutputStream对象座位参数,真正的写操作通过该对象去完成。例如ZipOutputStream,其本身只负责生成压缩格式的数据,至于这些数据是写到文件、内存、还是servletResponse,由输入的参数确定。这就是装饰器模式。

    Filter系列常用的有PrintStream(提供了print,println,write(boolean[int, char, string])各种操作,最终利用out.write方法以写字节的方式写进去。
    还有DataOutputStream,其提供了writeByte/writeBoolean/writeDouble/writeLong/wiretUTF等方法。
    还有就是socket/zip等不常用的。



    Java的流很方便也很复杂。复杂就复杂在实现一个功能往往需要多个类,而且有多种组合的办法。尚需继续在实践中总结。   

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值