通过IO流进行文件复制
通过IO流的类进行复制操作,将指定路径的文件复制到另一个盘符中
package org.jimmy.demo;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
/**
* @Author: Administrator
* @CreateTime: 2019-01-16 21:40
*/
public class IODemo4 {
public static void main(String[] args) throws IOException {
FileInputStream in = new FileInputStream("E:\\光辉岁月.mp4");
FileOutputStream out = new FileOutputStream("D:\\光辉岁月.mp4");
byte[] bytes = new byte[1024*8];//缓冲区,存够该长度的数据进入循环进行读写
int len=0;
//read()方法如果读不到回返回-1,以此作为进入循环条件
while((len=in.read(bytes))!=-1){
out.write(bytes,0,len);//写入bytes字节数组从0到len,即为刚才read()读取到的有效长度
out.flush();
}
out.close();
in.close();
}
}
执行结果为在D盘中也会复制一个该视频文件,我们可以通过对比两个文件的内存大小来判断是否复制完整