欢迎光临散文网 会员登陆 & 注册

字节缓冲流的性能体现

2020-04-01 20:47 作者:小垃圾kiki  | 我要投稿
package cn.jd.io;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

public class Copy {
    public static void main(String[] args) {
        long t1=System.currentTimeMillis();   
        copy("如何实现手机和电脑的蓝牙传输.mp4","如何实现手机和电脑的蓝牙传输2.mp4");
        long t2=System.currentTimeMillis();//好爽就是这么块
        System.out.println(t2-t1);
    }

    public static void copy(String srcPath, String destPath) {
        // 确定源
        //现在是没有使用缓冲流
        File src = new File(srcPath);// 读取的文件,使用输入流 因为是从文件到程序
        File src1 = new File(destPath);// 写入文件,输出流,从程序到文件
        // 确定流
        //这里我是对字节流的释放用了另一种方法
        try(InputStream  is=new BufferedInputStream(new FileInputStream(src));
                OutputStream  os=new BufferedOutputStream(new FileOutputStream(src1));) {
            
            
            byte[] flush = new byte[3];
//                System.out.println(flush.toString());     这里我们只是定义了,里面还没有内容
            int len = -1;
            // 确定方法
            while ((len = is.read(flush)) != -1) { // 通过is.read读取字节数组的长度
//                    System.out.println(len);
//                    System.out.println(flush.toString());   //这里我不知道怎么输出字符了
                os.write(flush, 0, len); // 将flush里面的字节写入到文件 ,这里要输入实际的长度
            }
            os.flush(); // 刷新缓冲流

        } catch (FileNotFoundException e) {

            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        

    }
}

字节缓冲流的性能体现的评论 (共 条)

分享到微博请遵守国家法律