• JAVA IO包的整理---------Writer和Reader


    一 Writer

      public abstract class Writer extends Object implements Appendable, Closeable, Flushable

      这个类表示的是输入字符的输入流.然后这个类中有一个同步锁,用来同步这个流内的操作.然后就是这个流和FileInputStream不同的是,它提供了append的方法来实现追加.

    然后也有flush,write,close的方法.然后就是既然可以写入字符,那其实也就是可以写入String了.因为这个类并不是直接与raw byte打交道,所以就使用默认的字符编码就行了.

      1.PrintWriter

       public class PrintWriter extends Writer

       这个类本身也不是一个Stream,也是提供了一些方法,可以想它内部含有的outputStream或者是Writer输出格式化的数据,如char sequence,String,int,float,Object等等,但是把raw byte输出.记住,这里是可以是任意的表示outputStream的对象.还提供了print,println等方法.

      2.OutputStreamWriter

      public class OutputStreamWriter extends Writer

      这个类主要是为了提供将字符编码的功能.我们可以指定使用的编码的方式,然后那编码过后的raw byte输出到outStream中,这个类本身也不是一个outputStream,需要使用外部的outputStream.

        a)FileWriter

            public class FileWriter extends OutputStreamWriter

            这个类主要是提供了将char sequence转化成buye[]输出到file中的能力,推测是它自己在内部构造了一个file的输出流.这个类使用默认的编码方式.

            PS:我们可以通过OutputStreamWriter和FileOutputStream结合使用来完成向文件中输出特定编码的数据的功能.

      3.BufferedWriter

      public class BufferedWriter extends Writer

      ok,这个类也是一个装饰类,主要是为了向其它writer来提供buff的功能,

      PS:这些装饰类可以嵌套使用,就可以实现很强的功能:如PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("foo.out"))); 在这个例子中,FileWriter是我们真正的writer,向文件中写入.其它来个只是为它提供一些功能,如buff,还有输出的格式等等.

      4.CharArrayWriter

        public class CharArrayWriter extends Writer

       ok,这个类和ByteArrayOutputStream的功能很相似,主要用来将text写入内置的char[],这个buff是可以自动长大的.然后也有toCharArray,toString等功能.也有writeto方法,可以将buff中的数据写入别的witer对象.

      5.FilterWriter

        public abstract class FilterWriter extends Writer

       首先注意到这个是一个abstract类,它也是一个装饰类,为其它writer提供一些额外的功能.ok,这个其实不应该写,它好像还没有子类.暂时不要用这个.

      6.StringWriter

        public class StringWriter extends Writer 

       ok,这个类可以用来构造String.它把写入的数据都存入内部的一个Stringbuff中,然后可以通过toString,getStringBuff来获得Stringbuff中的内容.同样的,close这个方法也没有什么卵用.

      7.PipedWriter

      public class PipedWriter extends Writer  

        这个和PipedOutputStream类似,只是这是把字符,或char[]输出到pipe中.

    二 Reader

     public abstract class Reader extends Object implements Readable, Closeable

      这个类和writer很类似,直接读进数据后,就转换成了char,或char[].

      好吧,这个和Writer很相似,就不一一做总结了

      

  • 相关阅读:
    hdu5728 PowMod
    CF1156E Special Segments of Permutation
    CF1182E Product Oriented Recurrence
    CF1082E Increasing Frequency
    CF623B Array GCD
    CF1168B Good Triple
    CF1175E Minimal Segment Cover
    php 正则
    windows 下安装composer
    windows apache "The requested operation has failed" 启动失败
  • 原文地址:https://www.cnblogs.com/zhaoxinshanwei/p/5692242.html
Copyright © 2020-2023  润新知