• IO流----缓冲流和转换流


    一.缓冲流涉及到的类:

    * BufferedInputStream
    * BufferedOutputStream
    * BufferedReader
    * BufferedWriter

    2.作用:
    作用:提供流的读取、写入的速度
    提高读写速度的原因:内部提供了一个缓冲区。默认情况下是8kb

     

     

    3.典型代码
    3.1 使用BufferedInputStream和BufferedOutputStream:处理非文本文件

     //缓冲流--字节型 实现 非文本文件复制
        @Test
        public void test01() {
            BufferedInputStream bis= null;
            BufferedOutputStream bos = null;
            try {
                //1.造文件
                File srcFile = new File("1.jpg");
                File destFile = new File("3.jpg");
                //2.造流
                FileInputStream fis = new FileInputStream(srcFile);
                FileOutputStream fos = new FileOutputStream(destFile);
                //2.1造缓冲流
                bis = new BufferedInputStream(fis);
                bos = new BufferedOutputStream(fos);
    
                //3.复制过程
                byte[] bytes = new byte[10];
                int len;
                while ((len=bis.read(bytes))!=-1){
                    bos.write(bytes,0,len);
                }
                System.out.println("复制完成");
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
            //4.关闭流,先关外层流再关内层流,关外层流时内层流会自动关闭
                if (bos!=null){
                    try {
                        bos.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
                if (bis!=null){
                    try {
                        bis.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
    
                }
            }
        }

    3.2 使用BufferedReader和BufferedWriter:处理文本文件

     //缓冲流--字符型 实现 文本文件复制
        @Test
        public void test01(){
            BufferedReader bif = null;
            BufferedWriter bof = null;
            try {
                bif = new BufferedReader(new FileReader(new File("C:\Users\Administrator\Desktop\1.txt")));
                bof = new BufferedWriter(new FileWriter(new File("C:\Users\Administrator\Desktop\2.txt")));
    
                char[] chars = new char[1024];
                int len;
                while ((len=bif.read(chars))!=-1){
                    bof.write(chars,0,len);
                }
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                if (bif!=null){
                    try {
                        bif.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
                if (bof!=null){
                    try {
                        bof.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        }

     

    二、转换流

    1.转换流涉及到的类:属于字符流
    InputStreamReader:将一个字节的输入流转换为字符的输入流
    解码:字节、字节数组 --->字符数组、字符串

    OutputStreamWriter:将一个字符的输出流转换为字节的输出流
    编码:字符数组、字符串 ---> 字节、字节数组

    说明:编码决定了解码的方式,使用OutputStreamWriter对字符进行编码时,若使用的是utf-8,那么使用InputStreamReader解码时也需要与编码所使用的字符集utf-8相同,否则会出现乱码。

    2.作用:提供字节流与字符流之间的转换

     

     4.典型实现:

    @Test
        public void test1() throws IOException {
    
            FileInputStream fis = new FileInputStream("dbcp.txt");
    //        InputStreamReader isr = new InputStreamReader(fis);//使用系统默认的字符集
            //参数2指明了字符集,具体使用哪个字符集,取决于文件dbcp.txt保存时使用的字符集
            InputStreamReader isr = new InputStreamReader(fis,"UTF-8");//使用系统默认的字符集
    
            char[] cbuf = new char[20];
            int len;
            while((len = isr.read(cbuf)) != -1){
                String str = new String(cbuf,0,len);
                System.out.print(str);
            }
    
            isr.close();
    
        }
    
    /*
    此时处理异常的话,仍然应该使用try-catch-finally
    
    综合使用InputStreamReader和OutputStreamWriter
     */
    @Test
    public void test2() throws Exception {
        //1.造文件、造流
        File file1 = new File("dbcp.txt");
        File file2 = new File("dbcp_gbk.txt");
    
        FileInputStream fis = new FileInputStream(file1);
        FileOutputStream fos = new FileOutputStream(file2);
    
        InputStreamReader isr = new InputStreamReader(fis,"utf-8");
        OutputStreamWriter osw = new OutputStreamWriter(fos,"gbk");
    
        //2.读写过程
        char[] cbuf = new char[20];
        int len;
        while((len = isr.read(cbuf)) != -1){
            osw.write(cbuf,0,len);
        }
    
        //3.关闭资源
        isr.close();
        osw.close();

    5.说明:
    //文件编码的方式(比如:GBK),决定了解析时使用的字符集(也只能是GBK)。

  • 相关阅读:
    POJ——3070Fibonacci(矩阵快速幂)
    NOJ——1568走走走走走啊走(超级入门DP)
    NOJ——1624死胡同(胡搞模拟)
    jmeter断言
    jmeter问题
    廖雪峰Java9正则表达式-2正则表达式进阶-3分组匹配
    廖雪峰Java9正则表达式-1正则表达式入门-2正则表达式匹配规则
    廖雪峰Java9正则表达式-1正则表达式入门-1正则表达式简介
    使用uiautomator2进行webview页面的测试
    廖雪峰Java8JUnit单元测试-2使用JUnit-4超时测试
  • 原文地址:https://www.cnblogs.com/lixia0604/p/13532326.html
Copyright © 2020-2023  润新知