Java实现文件拷贝(单线程与多线程)
- 将一个文件拷贝到另一个文件目录下,单线程实现涉及到文件IO的知识,下面是我写的源代码:
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;
public class SingleCopy {
public static void main(String[] args) {
long a=System.currentTimeMillis();//程序开始执行的时间
RandomAccessFile srcFile=null;
RandomAccessFile destFile=null;
try {
//定义原文件路径和目标文件路径
srcFile=new RandomAccessFile("E:\\学习资料\\图论\\JavaSE高级\\视频\\字符流.mp4","r");
destFile=new RandomAccessFile("F:\\FileCopy\\字符流.mp4","rw");
long startIndex=0;//源文件中赋值操作开始坐标
long endIndex= srcFile.length();//源文件中赋值操作结束坐标
srcFile.seek(startIndex);//将源文件中的指针移到开始坐标
destFile.seek(startIndex);//将目标文件的指针移到开始坐标
int len=-1;
byte[] bytes=new byte[1024];//1KB
while((len= srcFile.read(bytes))!=-1){
startIndex+=len;
destFile.write(bytes,0,len);
if(startIndex>=endIndex){
break;
}
}
} catch (FileNotFoundExceptio