字节流复制文件 自我见解

字节流的四种方式举例

使用字节流的四种方式进行复制文件,其中基本字节流:InputStream的作用表示那些从不同数据源产生输入的类,即其派生类多是不同数据源对应的流对象。,OutputStream的作用表示将数据写入不同的数据源。高效字节流利用了装饰器的原理,加入了缓冲区,分别将数据读入到缓冲区中,提取时从缓冲区中提取出来。

/*
 字节流四种方式复制文件
1、基本字节流一次读取一个字节
2、基本字节流一次读取一个字节数组
3、高效字节流一次读取一个字节
4、高效字节流一次读取一个字节数组
缓冲字节流(装饰流)
字符流(转换流=字节流+编码表)
注:我给的文件是1.pdf,大小为10.5 MB (11,061,898 字节)
 */
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;

public class InputStream{
    public static void main(String[] args) throws Exception{
        long begin=System.currentTimeMillis();
        //chuli1("D:\\1.mp3","D:\\Temp\\1.mp3");//通过应用程序对文件进行字节读写
        chuli2("D:\\1.mp3","D:\\Temp\\1.mp3");//通过操作系统,到应用程序对文件进行字节读写
        //chuli3("D:\\1.mp3","D:\\Temp\\1.mp3");//应用程序提供一个缓冲区
        //chuli4("D:\\1.mp3","D:\\Temp\\1.mp3");//操作系统提供一个缓冲区
        long end=System.currentTimeMillis();
        System.out.println("耗时:"+(end-begin)+"毫秒");
    }
    public static void  chuli1(String s,String d) throws Exception{
        FileInputStream fin=new FileInputStream(s);
        FileOutputStream fout=new FileOutputStream(d);
        int by=0;
        while ((by=fin.read())!=-1){
            fout.write(by);
        }
        fin.close();
        fout.close();
    }
    public static void chuli2(String s,String d)throws Exception{
        FileInputStream fin=new FileInputStream(s);
        FileOutputStream fout=new FileOutputStream(d);
        byte by[]=new byte[1024];
        int len;
        while ((len=fin.read())!=-1){
            fout.write(by,0,len);
        }
        fin.close();
        fout.close();
    }
    public static void chuli3(String s,String d)throws Exception{
        BufferedInputStream bis=new BufferedInputStream(new FileInputStream(s));
        BufferedOutputStream bos=new BufferedOutputStream(new FileOutputStream(d));
        int by=0;
        while ((by=bis.read())!=-1){
            bos.write(by);
        }
        bis.close();
        bos.close();
    }
    public static void chuli4(String s,String d)throws Exception{
        BufferedInputStream bis=new BufferedInputStream(new FileInputStream(s));
        BufferedOutputStream bos=new BufferedOutputStream(new FileOutputStream(d));
        byte by[]=new byte[1024];
        int len;
        while ((len=bis.read())!=-1){
            bos.write(by,0,len);
        }
        bis.close();
        bos.close();
    }
}

字节流读取一个字节的运行结果
字节流读取一个字节数组的结果
引入缓冲区,读取一个字节的结果
引入缓冲区,读取一个字节数组的结果

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值