Java实现将一个文件的复制到另一个文件中去(字节流形式...可复制文本,视频,音频等文件)

实现代码如下

package FileRead;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

public class Test {
    public static void main(String[] args) throws IOException {
        FileInputStream fileInputStream = new FileInputStream("D:\\file.mp4");
        FileOutputStream fileOutputStream = new FileOutputStream("D:\\myFile\\file.mp4");//使用时必须先创建文件夹myfile
        int b;
        while ((b = fileInputStream.read()) != -1){
            fileOutputStream.write(b);
        }

        fileInputStream.close();
        fileOutputStream.close();
    }
}

上述的实现代码只能对小型文件进行复制,但是像音频或者视频的话,一般文件较大,复制消耗的时间相当大,所以有了下面的改进版

package FileRead;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

public class Test {
    public static void main(String[] args) throws IOException {
//        FileInputStream fileInputStream = new FileInputStream("D:\\file.mp4");
//        FileOutputStream fileOutputStream = new FileOutputStream("D:\\myFile\\file.mp4");
//        int b;
//        while ((b = fileInputStream.read()) != -1){
//            fileOutputStream.write(b);
//        }
//
//        fileInputStream.close();
//        fileOutputStream.close();


        FileInputStream fileInputStream = new FileInputStream("D:\\file.mp4");
        FileOutputStream fileOutputStream = new FileOutputStream("D:\\myFile\\file.mp4");
        byte btye[] = new byte[1024];
        int len;
        while ((len = fileInputStream.read(btye)) != -1){
            fileOutputStream.write(btye,0,len);
        }

        fileInputStream.close();
        fileOutputStream.close();


    }
}

  • 2
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值