import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.DecimalFormat;
public class CopyFile extends Thread {
public File src;//源文件路径
public File dest;//复制目标路径
public CopyFile(String src,String dest) {
this.src = new File(src);
this.dest = new File(dest);
}
@Override
public void run() {
FileInputStream fis = null;
FileOutputStream fos = null;
try {
fis = new FileInputStream(src);
fos = new FileOutputStream(dest);
byte[] b = new byte[1024];//声明一个字节数组,每次读取的数据存到该字节数组里
int length=0;//返回每次读取的数据长度
long len = src.length();//获取源文件的长度
double temp = 0;
DecimalFormat df = new DecimalFormat("##%");
while((length=fis.read(b))!=-1){
fos.write(b, 0, length);//把每次读取的内容,输出到目标路径文件中
temp+=length;//把每次读取的数据长度累加
double d = temp/len;//计算出已经读取的长度占文件总长度的比率
int jd = (int) d;
if(jd%10==0){
System.out.println(src.getName()+"文件复制了:"+df.format(d));
}
}
System.out.println(src.getName()+"复制完毕!");
} catch (IOException e) {
e.printStackTrace();
}finally{
try {
fis.close();
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
//测试部分
public class CopyTest {
public static void main(String[] args) {
CopyFile cf1 = new CopyFile("E:\\a.txt","F:\\a.txt");
CopyFile cf2 = new CopyFile("E:\\b.txt","F:\\b.txt");
CopyFile cf3 = new CopyFile("E:\\c.txt","F:\\c.txt");
// CopyFile cf4 = new CopyFile("E:\\d.txt","F:\\d.txt");
// CopyFile cf5 = new CopyFile("E:\\e.txt","F:\\e.txt");
// CopyFile cf6 = new CopyFile("E:\\java.wmv","F:\\java.wmv");
cf1.start();
cf2.start();
cf3.start();
// cf4.start();
// cf5.start();
// cf6.start();
}
}