zoukankan      html  css  js  c++  java
  • 缓冲流复制文件与基本流复制文件比较

    import javafx.scene.control.ButtonBar;
    
    import java.io.*;
    
    public class Demo03Copy2 {
        public static void main(String[] args) throws IOException {
            long s=System.currentTimeMillis();
    
            BufferedInputStream bis=new BufferedInputStream(new FileInputStream("F:\basic\untitled13\src\it\cast\day15\demo02\1.jpeg"));
            BufferedOutputStream bos=new BufferedOutputStream(new FileOutputStream("F:\basic\untitled13\src\it\cast\day15\demo02\3.jpeg"));
            /*
            int len=0;
            while ((len=bis.read())!=-1){
                bos.write(len);
            }//复制文件共耗时:34毫秒*/
    
            byte[] bytes1=new byte[1024];
            int len=0;
            while ((len=bis.read(bytes1))!=-1){
                //System.out.println(new String(bytes1,0,len));//读取有效的几个
                bos.write(bytes1,0,len);
            }//复制文件共耗时:5毫秒
    
            bis.close();
            bos.close();
            long e=System.currentTimeMillis();
            System.out.println("复制文件共耗时:"+(e-s)+"毫秒");
        }
    }

    基本流复制文件:

    import java.io.FileInputStream;
    import java.io.FileOutputStream;
    import java.io.IOException;
    
    //文件的复制
    public class Demo03Copy {
        public static void main(String[] args) throws IOException {
            long s=System.currentTimeMillis();
            FileInputStream fis=new FileInputStream("F:\basic\untitled13\src\it\cast\day15\demo02\1.jpeg");
            FileOutputStream fos=new FileOutputStream("F:\basic\untitled13\src\it\cast\day15\demo02\2.jpeg");
    /*        byte[] bytes1=new byte[1024];
            int len=0;
            while ((len=fis.read(bytes1))!=-1){
                //System.out.println(new String(bytes1,0,len));//读取有效的几个
                fos.write(bytes1,0,len);
            }//复制文件共耗时:10毫秒*/
    
            int len=0;
            while((len=fis.read())!=-1){
                fos.write(len);
    
            }//复制文件共耗时:4502毫秒
            fos.close();
            fis.close();
            long e=System.currentTimeMillis();
            System.out.println("复制文件共耗时:"+(e-s)+"毫秒");
    
        }
    }

     复制的文件大小是359 KB (367,976 字节)的jpeg格式的图片,在同一文件夹下面复制文件。

  • 相关阅读:
    菜鸟笔记 -- Chapter 6.2.6 内部类
    菜鸟笔记 -- Chapter 6 面向对象
    菜鸟笔记 -- Chapter 6.1 面向对象概述
    菜鸟笔记 -- Chapter 4.7 代码注释与编码规范
    菜鸟笔记 -- Chapter 4 Java语言基础
    小白袍 -- Chapter 1.4.1.1 URL编码的理论解读
    小白袍 -- Chapter 1.1 避不开的编解码
    小白袍 -- Chapter 1 Java中的Encode与Decode
    菜鸟崛起 Ajax
    软件测试
  • 原文地址:https://www.cnblogs.com/cy2268540857/p/13797664.html
Copyright © 2011-2022 走看看