1:项目结构
2:代码
1:CopyFile
package tianyi.demo4;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.DecimalFormat;
public class CopyFile extends Thread {
public File older;// 源文件路径
public File newer;// 复制目标路径
public CopyFile(String older, String newer) {
this.older = new File(older);
this.newer = new File(newer);
}
@Override
public void run() {
FileInputStream fis = null;
FileOutputStream fos = null;
try {
fis = new FileInputStream(older);
fos = new FileOutputStream(newer);
byte[] b = new byte[1024];// 声明一个字节数组,每次读取的数据存到该字节数组里
int length = 0;// 返回每次读取的数据长度
long len = older.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(older.getName() + "文件复制了:" + df.format(d));
}
}
System.out.println(older.getName() + "复制完毕!");
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
fis.close();
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
2:Test
package tianyi.demo4;
public class Test {
public static void main(String[] args) {
CopyFile cf1 = new CopyFile("F:\\helloworld\\hang1.txt", "F:\\helloworld\\jia1.txt");
CopyFile cf2 = new CopyFile("F:\\helloworld\\hang2.txt", "F:\\helloworld\\jia2.txt");
CopyFile cf3 = new CopyFile("F:\\helloworld\\hang3.txt", "F:\\helloworld\\jia3.txt");
cf1.start();
cf2.start();
cf3.start();
}
}
3:效果展示