public class ThreadCopy2 {
static List list = new ArrayList();
public static void main(String[] args) {
SrcReader sr = new SrcReader();
SrcWriter sw = new SrcWriter();
System.out.println("!!!请在10秒内输入要复制的文件路径");
sr.start();
list = sr.readerSrc();
//线程休眠
try {
Thread.sleep(10000);
} catch (InterruptedException e) {
e.printStackTrace();
}
sw.start();
}
}
/**
* 读取文件
*/
class SrcReader extends Thread{
List<Integer> list = new ArrayList<Integer>();
public List readerSrc(){
return list;
}
@Override
public void run() {
Scanner input = new Scanner(System.in);
//提醒用户输入复制文件的路径
System.out.println("请您输入您需要复制的文件路径:");
String copyFilePath;
File copyFile;
copyFilePath = input.next();
copyFile = new File(copyFilePath);
if(!copyFile.exists()){
System.out.println("您输入的文件路径不存在,程序已退出!");
System.exit(0);
}
System.out.println(copyFile.getName()+"文件存在!");
FileInputStream fi = null;
try {
fi = new FileInputStream(copyFile);
byte[] bytes = new byte[1024];
int i ;
double copyLength = 0;
long len = copyFile.length();
DecimalFormat d = new DecimalFormat("00.##%");
while((i = fi.read(bytes)) != -1){
list.add(i);
copyLength += i;
double num = copyLength/len;
System.out.println(copyFile.getName()+"已复制的进度:"+d.format(num));
}
System.out.println("文件复制成功");
}catch(IOException e) {
e.printStackTrace();
}finally {
//关闭流
try {
fi.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
实现效果public class ThreadCopy2 { static List list = new ArrayList(); public static void main(String[] args) { SrcReader sr = new SrcReader(); SrcWriter sw = new SrcWriter(); System.out.println("!!!请在10秒内输入要复制的文件路径"); sr.start(); list = sr.read