A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 忆々疯ラ萧萧 中级黑马   /  2016-4-28 20:13  /  599 人查看  /  0 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

IO流:

水流:

        分类:
       
        从不同角度:
                1. 流的方向:  输入输出
                2. 处理单位:  字节字符
                3. 功能:节点和处理流
               
        4个流的抽象基类:
       
                                字节                        字符                        具有的行为:
        输入                InputStream                Reader                        读(read)
        输出                OutputStream         Writer                        写(write)
       
       
       
        流都有close方法:
       
        InputStream: 字节输入流:
                int read();表示读取文件指针后的一个字节
               
                int read(byte[] b): 表示读取b.length个字节, 返回实际读取的字节数,
                                若返回的数大于0,表示没有读完;
                               
                                byte[] b = new byte[1024];
                                int len = 0;
                                while((len= in.read(b)) > 0){
                                        //读完之后, 数据在  b这个缓冲区里;
                                }
                               
                int read(byte[] b, int offset,  int len):表面从b的第offset位置开始读,读了len个;
               
       
        Reader: 字符输入流:
                int read();表示读取文件指针后的一个字符
               
                int read(char[] b): 表示读取b.length个字节, 返回实际读取的字符数,
                                若返回的数大于0,表示没有读完;
                               
                                char[] b = new char[1024];
                                int len = 0;
                                while((len= in.read(b)) > 0){
                                        //读完之后, 数据在  b这个缓冲区里;
                                }
                               
                int read(char[] b, int offset,  int len):表面从b的第offset位置开始读,读了len个;
               
       
        输出流有flush:
        OutputStream: 字节的输出流:
               
                void write(int b):向外写出一个字节
               
                void write(byte[] b): 向外写b.length个字节;
               
                void write(byte[] b, int offset, int len:
                       
        Writer:
                void write(int b):向外写出一个字符
               
                void write(char[] b): 向外写b.length个字符;
               
                void write(char[] b, int offset, int len):
               
                void write(String str):
                void write(String str,int offset, int len):
               
               
        文件流:
                FileInputStream:需要源
                        new FileInputStream("src.txt")
               
                FileOutputStream:需要目标
                        new FileOutputStream("dest.txt");
                        new FileOutputStream("dest.txt",true);//true表示在原文件内容后面继续添加内容
               
                FileReader:需要源
                        new FileReader();
               
                FileWriter:        需要目标
                        FileWriter(String filename, boolean append);
                       
        操作流的步骤:
       
                1.        找到源或目标
                2.        创建输入输出管道,并和源或目标连接上       
                3.        IO操作
                4.        关闭资源
               
               
                文件拷贝:
               
                public  void   copy(){
                       
                        File src = new File("src.txt");
                        File dest = new File("dest.txt");
                       
                        if(!src.exists() || !src.isFile())
                        {
                                return;
                        }
                        InputStream in = null;
                        OutputStream out = null;
                        try{
                                in = new FileInputStream(src);
                                out = new FileOutputStream(dest);
                               
                               
                                byte[] b = new byte[1024];//开辟大小为1024个字节的缓冲区
                                int len= 0;//表示每次读了多少个字节
                               
                                while((len = in.read(b)) != -1)
                                {
                                        //每次读完之后,数据存在缓冲区b里面
                                        out.write(b,0,len);
                                }
                        }catch(IOException e){
                                //异常处理
                        }finally{
                                try{
                                        if(out!= null)
                                        {       
                                                out.close();
                                        }
                                }catch(IOException e)
                                {
                                        //处理异常
                                }finally{
                                        if(in != null)
                                        {       
                                                try{
                                                        in.close();
                                                }catch(IOException e)
                                                {
                                                        //处理异常
                                                }
                                        }
                                }       
                        }
                }
               
               
                Java7的自动资源关闭:
                        java7开始出现:java.lang.AutoCloseable接口,Closable继承于AutoCloseable接口;
                        凡是实现了该接口的资源,就可以不再手动去关闭资源:
                       
                        以前的格式:
                               
                                try
                                {       
                                        //可能出现异常的代码
                                }
                                catch(IOException e)
                                {
                                        //异常处理
                                }finally{
                                        //关闭资源
                                }
                        现在的格式:
                       
                                try
                                        (
                                                声明或创建(打开)资源,(资源的类必须实现java.lang.AutoCloseable接口)
                                               
                                                //IO操作不行
                                        )
                                {       
                                        //可能出现异常的代码       
                                }
                                catch(IOException e)
                                {
                                        //异常处理
                                }
                       
                        public  void   copy(){
                       
                                File src = new File("src.txt");
                                File dest = new File("dest.txt");
                               
                                if(!src.exists() || !src.isFile())
                                {
                                        return;
                                }
                                       
                                //=========================================
                               
                                try
                                        (
                                                InputStream in = new FileInputStream(src);
                                                OutputStream out  = new FileOutputStream(dest);
                                        )
                                {
                                        byte[] b = new byte[1024];
                                        int len = 0;
                                       
                                        while((len= in.read(b)) != -1)
                                        {
                                                out.write(b,0,len);
                                        }
                                }
                                catch(IOException e)
                                {
                                        //异常处理
                                }
                               
                                //=========================================
                        }
               
       
        字符输出流和字节输出流的区别:
       
                字符输出会使用到缓冲区,就必须刷新或关闭,字节不会,
               
               
               
        操作流,不管是什么流,最保险的方式,就是最后都close一下.
       
               

0 个回复

您需要登录后才可以回帖 登录 | 加入黑马