题目:使用多线程实现多个文件同步复制功能,并在控制台显示复制的进度,进度以百分比表示。例如:把文件A复制到E盘某文件夹下,在控制台上显示“XXX文件已复制10%”,“XXX文件已复制20%”……“XXX文件已复制100%”,“XXX复制完成!”
代码如下:
1、Runnable接口方式
package com.day505.testdemo.exam;
import java.io.*;
import java.text.DecimalFormat;
public class FileCopy implements Runnable {
//表示源文件
public File oldFile;
//表示目标文件
public File newFile;
public FileCopy(String oldFile, String newFile) {
this.oldFile = new File(oldFile);
this.newFile = new File(newFile);
}
@Override
public void run() {
//创建文件输入输出流
FileInputStream fileInputStream = null;
FileOutputStream fileOutputStream = null;
try {
fileInputStream = new FileInputStream(oldFile);
fileOutputStream = new FileOutputStream(newFile);
//声明一个字节数组,将每次读取的数据放入 字节数组的长度自己设定
//如果要读取的文件中的数据比较少的话,建议设的值小一些,这样可以看清楚复制进度
byte[] bytes = new byte[512];
//返回每次读取的长度
int length;
//得到源文件的字节长度
long len = oldFile.length();
double temp = 0;
//数据格式化
DecimalFormat fd = new DecimalFormat("##.##%");
//从数组中读取数据并返回长度
while ((length = fileInputStream.read(bytes))!= -1){
//将数组中的数据放入到目标路径中去,数据长度是length
fileOutputStream.write(bytes,0,length);
//把每次读取到的数据累加
temp = temp+length;
//将读取到的长度/文件总长度
double d = temp/len;
System.out.println(oldFile.getName()+"文件复制到"+newFile.getName()+"已经复制了"+fd.format(d));
}
System.out.println(oldFile.getName()+"文件复制完毕");
}catch (IOException e) {
e.printStackTrace();
} finally {
try {
fileInputStream.close();
fileOutputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) {
FileCopy fileCopy = new FileCopy("D:\\a.txt","D:\\b.txt");
FileCopy fileCopy2 = new FileCopy("D:\\c.txt","D:\\d.txt");
FileCopy fileCopy3 = new FileCopy("D:\\e.txt","D:\\f.txt");
new Thread(fileCopy).start();
new Thread(fileCopy2).start();
new Thread(fileCopy3).start();
}
}
2、继承Thread方式
package com.day505.testdemo.exam;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.DecimalFormat;
public class FileCopy2 extends Thread {
//表示源文件
public File oldFile;
//表示目标文件
public File newFile;
public FileCopy2(String oldFile, String newFile) {
this.oldFile = new File(oldFile);
this.newFile = new File(newFile);
}
@Override
public void run() {
//创建文件输入输出流
FileInputStream fileInputStream = null;
FileOutputStream fileOutputStream = null;
try {
fileInputStream = new FileInputStream(oldFile);
fileOutputStream = new FileOutputStream(newFile);
//声明一个字节数组,将每次读取的数据放入 字节数组的长度自己设定
//如果要读取的文件中的数据比较少的话,建议设的值小一些,这样可以看清楚复制进度
byte[] bytes = new byte[512];
//返回每次读取的长度
int length;
//得到源文件的字节长度
long len = oldFile.length();
double temp = 0;
//数据格式化
DecimalFormat fd = new DecimalFormat("##.##%");
//从数组中读取数据并返回长度
while ((length = fileInputStream.read(bytes))!= -1){
//将数组中的数据放入到目标路径中去,数据长度是length
fileOutputStream.write(bytes,0,length);
//把每次读取到的数据累加
temp = temp+length;
//将读取到的长度/文件总长度
double d = temp/len;
System.out.println(oldFile.getName()+"文件复制到"+newFile.getName()+"已经复制了"+fd.format(d));
}
System.out.println(oldFile.getName()+"文件复制完毕");
}catch (IOException e) {
e.printStackTrace();
} finally {
try {
fileInputStream.close();
fileOutputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) {
FileCopy2 fileCopy = new FileCopy2("D:\\a.txt","D:\\b.txt");
FileCopy2 fileCopy2 = new FileCopy2("D:\\c.txt","D:\\d.txt");
FileCopy2 fileCopy3 = new FileCopy2("D:\\e.txt","D:\\f.txt");
fileCopy.start();
fileCopy2.start();
fileCopy3.start();
}
}
运行结果
e.txt文件复制到f.txt已经复制了27.02%
c.txt文件复制到d.txt已经复制了39.45%
a.txt文件复制到b.txt已经复制了5.25%
e.txt文件复制到f.txt已经复制了54.04%
c.txt文件复制到d.txt已经复制了78.89%
a.txt文件复制到b.txt已经复制了10.5%
c.txt文件复制到d.txt已经复制了100%
e.txt文件复制到f.txt已经复制了81.06%
a.txt文件复制到b.txt已经复制了15.74%
e.txt文件复制到f.txt已经复制了100%
e.txt文件复制完毕
c.txt文件复制完毕
a.txt文件复制到b.txt已经复制了20.99%
a.txt文件复制到b.txt已经复制了26.24%
a.txt文件复制到b.txt已经复制了31.49%
a.txt文件复制到b.txt已经复制了36.73%
a.txt文件复制到b.txt已经复制了41.98%
a.txt文件复制到b.txt已经复制了47.23%
a.txt文件复制到b.txt已经复制了52.48%
a.txt文件复制到b.txt已经复制了57.73%
a.txt文件复制到b.txt已经复制了62.97%
a.txt文件复制到b.txt已经复制了68.22%
a.txt文件复制到b.txt已经复制了73.47%
a.txt文件复制到b.txt已经复制了78.72%
a.txt文件复制到b.txt已经复制了83.96%
a.txt文件复制到b.txt已经复制了89.21%
a.txt文件复制到b.txt已经复制了94.46%
a.txt文件复制到b.txt已经复制了99.71%
a.txt文件复制到b.txt已经复制了100%
a.txt文件复制完毕