• JAVA之用缓冲流读写文件


    public class CopyDemo {
    public static void main(String[] args) throws Exception{
    long time1 = System.currentTimeMillis();
    copy4(new File("d:\ccc.mp4"),new File("e:\ccc.mp4"));
    long time2 = System.currentTimeMillis();
    System.out.println(time2-time1);
    }
    //1.字节流读写单个字节 太慢了
    public static void copy1(File src,File desc) throws Exception{
    FileInputStream fis = new FileInputStream(src);
    FileOutputStream fos = new FileOutputStream(desc);
    int len = 0;
    while ((len=fis.read())!=-1){
    fos.write(len);
    }
    fos.close();
    fis.close();
    }
    //2.字节流读写字节数组 550
    public static void copy2(File src,File desc) throws Exception{
    FileInputStream fis = new FileInputStream(src);
    FileOutputStream fos = new FileOutputStream(desc);
    int len = 0;
    byte[] b = new byte[1024*10];
    while ((len=fis.read(b))!=-1){
    fos.write(b,0,len);
    }
    fos.close();
    fis.close();
    }
    //3.字节流缓冲区 读写单个字节 4252
    public static void copy3(File src,File desc) throws Exception{
    BufferedInputStream bis = new BufferedInputStream(new FileInputStream(src));
    BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(desc));
    int len = 0;
    while ((len=bis.read())!=-1){
    bos.write(len);
    }
    bis.close();
    bos.close();
    }
    //4.字节流缓冲区读写字节数组 585
    public static void copy4(File src,File desc) throws Exception{
    BufferedInputStream bis = new BufferedInputStream(new FileInputStream(src));
    BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(desc));
    int len = 0;
    byte[] b = new byte[1024*10];
    while ((len=bis.read(b))!=-1){
    bos.write(b,0,len);
    }
    bos.close();
    bis.close();
    }
  • 相关阅读:
    小端字节序与大端字节序
    V8引擎的垃圾回收策略
    TTL 和 DNS TTL 的区别
    详解 undefined 与 null 的区别
    Node.js 事件循环机制
    requestAnimationFrame 知多少?
    Web前端知识体系精简
    Vue.js 和 MVVM 小细节
    使用 Node.js 搭建 Web 服务器
    H5单页面手势滑屏切换原理
  • 原文地址:https://www.cnblogs.com/Y-mmeng/p/10606464.html
Copyright © 2020-2023  润新知