Java运用缓冲流进行拷贝文本文件

本文档展示了如何使用Java的BufferedInputStream和BufferedOutputStream进行文本文件的高效拷贝,通过实例代码演示了从D盘读取并写入到E盘的时间,并计算了整个操作耗时。
摘要由CSDN通过智能技术生成

Java运用缓冲流进行拷贝文本文件

代码如下:

import org.junit.Test;

import java.io.*;
import java.text.SimpleDateFormat;

public class E {

    @Test
    public void copy() throws IOException {
        long start=System.currentTimeMillis();
        BufferedInputStream bis=new BufferedInputStream(new FileInputStream("d://1.wmv"));//从D盘读取出数据
        BufferedOutputStream bos=new BufferedOutputStream(new FileOutputStream("e://1.wmv"));//写入另一个文本文件中
        byte []buffer=new byte[1024];
        int n=-1;
        while((n=bis.read(buffer))!=-1){
            bos.write(buffer);
        }
        long end=System.currentTimeMillis();
        SimpleDateFormat sdf=new SimpleDateFormat("ss");
        System.out.println(sdf.format(end-start));//显示整个拷贝过程时间
    }

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值