字节流以及缓冲流

在这里插入图片描述
如果我们用字节流处理查看文本可能会出现乱码,而复制文本不会因为我们汉字是3个字节,我们byte容量不一定是3的倍数

import java.io.*;
public class TestInputOutput {
    public static void main(String[] args) {
        //跟字符流操作的步骤一样
        TestInputOutput t=new TestInputOutput();
        long st=System.currentTimeMillis();
        t.test("D:\\mycode\\src\\千玺.jpeg","D:\\mycode\\src\\千玺2.jpeg");
        long et=System.currentTimeMillis();
        System.out.println("复制时间"+(et-st));//555
    }
    public void test(String p1,String p2){
        //创建文件流
        File srcF = new File(p1);
        File desF = new File(p2);
        //2.创建字节流
        FileInputStream fis = null;
        FileOutputStream fos = null;
        try {
            fis = new FileInputStream(srcF);
            fos = new FileOutputStream(desF);
           //复制过程
            byte[] buf = new byte[5];//如果文件或者视频比较大的话我们可以调节数组大小
            int tem;
            while ((tem = fis.read(buf)) != -1) {
                fos.write(buf, 0, tem);
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (fis != null) {
                try {
                    fis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                if (fos != null) {
                    try {
                        fos.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    }
}

缓冲流最大的作用就是加快我们处理的速度

import java.io.*;

public class TestIo02 {
    public static void main(String[] args) {
        //字节缓冲流与字符流对比
        TestIo02 t=new TestIo02();
        long st=System.currentTimeMillis();
        t.test("D:\\mycode\\src\\千玺.jpeg","D:\\mycode\\src\\千玺3.jpeg");

        long et=System.currentTimeMillis();
        System.out.println("复制时间"+(et-st));//10
    }
    public void test(String p1,String p2){
        //创建文件流
        File srcF = new File(p1);
        File desF = new File(p2);
        //2.创建字节流
        FileInputStream fis = null;
        FileOutputStream fos = null;
        BufferedInputStream bis=null;
        BufferedOutputStream bos=null;
        try {
            fis = new FileInputStream(srcF);
            fos = new FileOutputStream(desF);
            //创建缓存流
            bis=new BufferedInputStream(fis);
            bos=new BufferedOutputStream(fos);
            //复制过程
            byte[] buf = new byte[5];//如果文件或者视频比较大的话我们可以调节数组大小
            int tem;
            while ((tem = bis.read(buf)) != -1) {
                bos.write(buf, 0, tem);
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            //我们虽然打开了4个流但是我们外层的缓冲流关闭后内部文件流也关闭了
            // 所以我们只需要关闭缓冲流就好
            if (bis != null) {
                try {
                    bis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                if (bos != null) {
                    try {
                        bos.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    }
}

缓冲流中字符流就不写了都差不多
读写操作细节说一下
我们字符流用char一般长文本都填1024或者8192在这里插入图片描述在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值