Java:缓冲流、转换流、序列化流

1、缓冲流-概述读、写都可以使用一种“数组”的方式,这种方式会大大提高读、写的效率,基于这种原因,Java内部提供一种“缓冲流”,其内部自带一个缓冲区(数组),目的也是为了提高程序运行速率。2、缓冲流-字节缓冲流1)、输出流:BufferedOutputStream2)、输入流:BuferedInputStream注意:都没有特有的方法,都是继承于父类的方法public static ...
摘要由CSDN通过智能技术生成

1、缓冲流-概述

读、写都可以使用一种“数组”的方式,这种方式会大大提高读、写的效率,基于这种原因,Java内部提供一种“缓冲流”,其内部自带一个缓冲区(数组),目的也是为了提高程序运行速率。

2、缓冲流-字节缓冲流

1)、输出流:BufferedOutputStream

2)、输入流:BuferedInputStream

注意:都没有特有的方法,都是继承于父类的方法

public static void main(String[] args) {
    

    try (
            //1.源文件到程序的输入流:
            BufferedInputStream bufIn = new BufferedInputStream(
					new FileInputStream("d:\\douyu.exe"));
            //2.程序到目标文件的:输出流
            BufferedOutputStream bufOut = new BufferedOutputStream(
					new FileOutputStream("e:\\douyu_copy.exe"))
    ) {
    
        //一次读写一个字节
        /*
        int b = 0;
        while ((b = bufIn.read()) != -1) {//从"缓存区读",一次读一个字节
            bufOut.write(b);//输出到"缓存区",一次输出一个字节
        }
        */
        //一次读写一个字节数组
        byte[] byteArray = new byte[1024];
        int len = 0;
        while ((len = bufIn.read(byteArray)) != -1) {
    //从"缓存区读",一次读1K
            bufOut.write(byteArray,0,len);//输出到”缓存区",一次输出1k
        }

        System.out.println("复制完毕!");
    } catch (FileNotFoundException e) {
    
        e.printStackTrace();
    } catch (IOException e) {
    
        e.printStackTrace();
    }
}

在这里插入图片描述

3、缓冲流-字符缓冲流

1)、字符缓冲流同“字节缓冲流”作用一样,内部提供了8K的缓冲区;

2)、字符缓冲流的俩个类各自提供了一个特有的方法:

输出流:newLine();//换行

输入流:readLine();//忽略换行

配套使用

public static void main(String[] args) {
    
    try (BufferedReader bufIn = new BufferedReader(
				new FileReader("demo03.txt"));
         BufferedWriter bufOut = new BufferedWriter(
				new FileWriter(("demo3_copy2.txt")))
    ) {
    

        //一次读写一个字符数组
        /*char[] chArray = new char[1024];
        int len = 0;
        while ((len = bufIn.read(chArray)) != -1) {
            bufOut.write(chArray,0,len);
        }*/

        //使用特有方法赋值
        String row = null;
        while ((row = bufIn.readLine()) != null) {
    //一次读取一行
            bufOut.write(row);
            bufOut.newLine();//输出一个换行
        }
    } catch (FileNotFoundException e) {
    
        e.printStackTrace();
    } catch (IOException e) {
    
        e.printStackTrace();
    }
}

4、缓冲流-练习-文本排序-分析

在这里插入图片描述

5、缓冲流-练习-文本排序-实现

public static void main(String[] args) throws IOException {
    
    //1.源文件的输入流
    BufferedReader bufIn = new BufferedReader(new FileReader("demo05.txt"));
    //2.目标文件的输出流
    BufferedWriter bufOut = 
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值