io流基础认知

来源:互联网 发布:步步高i270软件下载 编辑:程序博客网 时间:2024/06/06 02:34
一、流是什么:

    流:程序与设备数据传输的管道


二、流的分类:

      1、   分类:四大基本抽象流、文件流、缓冲流、转换流、数据流、print流、object流
         四大基本抽象流:         字节流              字符流
                 输入流         InputStream           Reader
                 输出流         outputStream          Writer
                 

      2、   节点流、处理流:节点流即原始流,处理流是套在原始流或者处理流上的管道也称为包裹流。

               常用流的方法

                 InputStream:
                         public int read ();
                         public int read(byte[] b)
                         f.close();
                 outputStream:
                         void write();
                         void write(byte[] b)
                         void cloe();
                         void flush();   //刷新缓冲区
                 Reader:
                         int read()
                         int read(char[] cbuf)
                         void close();
                         long skip(long n)   //跳过N个字节
                 Writer: 
                         void write(int c)
                         void write(char[] cbuf)
                         void write(String string)
                         void close();
                         void flush();
 
          缓冲流:带缓冲区的输入输出流,减少IO访问次数,保护硬盘。缓冲流是处理流。
                         BufferReader(Read in)
                         BufferReader(Read in, int size)
                      BufferedInputStream bis = new(new(FileInputStream("d:\\....\\123.txt")))
                        BufferedOutputStream bis = new(new(FileoutputStream("d:\\....\\123.txt")))
                        len = bis.read(buf);
                        while(-1 = len){
                              bos.write(buf , 0 ,len)
                              len = bis.read(buf);
                         }
                                         
         数据流:DataInputStream 、DataOutputStream 是处理流
         转换流:OutStreamWriter是把OutputStream流转换成writer流的流
         print流:PrintStream、Printwriter,只有输出流是处理流
             输入输出的重定向:PrintStream ps = new PrintStream("d:\\.....\\123.txt")
                               System.setOut(ps);//System.setErr(ps)
                               System.out.println("xxxx")   //将在123.txt文件中输出
         
         对象流:ObjectOutStream(OutputStream)
                 ObjectInputStream(InputStream)
         作用:把一个object直接转换为字节流,然后写入在硬盘或网络中

           注意: 必须实现Serializable接口,空接口,仅起到标识作用,类中被transient修饰的成员变量不能被序列化

三、常用用法:

      1、 String s = "aaaaaa";

             s.getBytes();    //把s变成byte数组

      2、Byte[] buf = new Byte[1024];

            String s = new String(buf);   // 把数组变成字符串

       3、获取字符串的字节数

             s.getBytes().length;




0 0
原创粉丝点击