加入收藏 | 设为首页 | 会员中心 | 我要投稿 北几岛 (https://www.beijidao.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

字节流复制文件-字节流

发布时间:2021-07-06 05:38:52 所属栏目:大数据 来源: https://blog.csdn.net/summoxj
导读:import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; /* ?* 字节流复制文件 ?* 采用数组缓冲提高效率 ?* FileInputStream 读取字节数组;FileOutputStream 写字节数组? ? ?*/ public class Copy_1 { ?? ?public s
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

/*
?* 字节流复制文件
?* 采用数组缓冲提高效率
?* FileInputStream 读取字节数组;FileOutputStream 写字节数组? ?
?*/
public class Copy_1 {
?? ?public static void main(String[] args){
?? ??? ?long s=System.currentTimeMillis();
?? ??? ?FileInputStream fis=null;
?? ??? ?FileOutputStream fos=null;
?? ??? ?try {
?? ??? ??? ?fis=new FileInputStream("d:demoa.txt");
?? ??? ??? ?fos=new FileOutputStream("d:demob.txt");
?? ??? ??? ?//定义字节数组,缓冲
?? ??? ??? ?byte[] bytes=new byte[1024];
?? ??? ??? ?//读取数组,写入数组
?? ??? ??? ?int len=0;
?? ??? ??? ?while((len=fis.read(bytes))!=-1){
?? ??? ??? ??? ?fos.write(bytes,len);
?? ??? ??? ?}
?? ??? ?} catch (IOException ex) {
?? ??? ??? ?System.out.println(ex);
?? ??? ??? ?throw new RuntimeException("文件复制失败");
?? ??? ?}finally{
?? ??? ??? ?try {
?? ??? ??? ??? ?if(fos!=null)
?? ??? ??? ??? ??? ?fos.close();
?? ??? ??? ?} catch (IOException ex) {
?? ??? ??? ??? ?throw new RuntimeException("释放资源失败");
?? ??? ??? ?}finally{
?? ??? ??? ??? ?try {
?? ??? ??? ??? ??? ?if(fis!=null)
?? ??? ??? ??? ??? ??? ?fis.close();
?? ??? ??? ??? ?} catch (IOException ex) {
?? ??? ??? ??? ??? ?throw new RuntimeException("释放资源失败");
?? ??? ??? ??? ?}
?? ??? ??? ?}
?? ??? ?}
?? ??? ?long e=System.currentTimeMillis();
?? ??? ?System.out.println();
?? ?}

}



字节流和字节复制的速度对比:

一个20M文件:字节流70mm,字节40000mm

(编辑:北几岛)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读