package thread0103;
public class Downtest {
public static void main(String[] args) {
Down d1=new Down("E:\\TTKVOD_CACHE\\画江湖之不良人第二季24.mkv", "F:\\画江湖之不良人第二季24.mkv");
d1.setName("画江湖之不良人第二季24");
d1.start();
Down d2=new Down("E:\\TTKVOD_CACHE\\今晚80后脱口秀HDTV20161229.mkv", "F:\\今晚80后脱口秀HDTV20161229.mkv");
d2.setName("今晚80后脱口秀HDTV20161229");
d2.start();
Down d3=new Down("E:\\TTKVOD_CACHE\\蓝色大海的传说13.rmvb", "F:\\蓝色大海的传说13.rmvb");
d3.setName("蓝色大海的传说13.rmvb");
d3.start();
}
}
package thread0103;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.DecimalFormat;
/**
* 使用多线程模拟迅雷下载的进度,用百分比表示,例如:
小黄人大眼萌,已下载xx%
龙门客栈,已下载xx%
黄飞鸿,已下载xx%
* @author Administrator
*/
public class Down extends Thread {
public File yfile;
public File mbfile;
public static DecimalFormat df=new DecimalFormat("#.##%");
public Down(String yfile, String mbfile) {
super();
this.yfile =new File(yfile);
this.mbfile = new File(mbfile);
}
@Override
public void run() {
FileInputStream fis=null;
FileOutputStream fos=null;
try {
fis=new FileInputStream(yfile);
fos=new FileOutputStream(mbfile);
if(!mbfile.exists()){
mbfile.mkdir();
}
} catch (FileNotFoundException e) {
System.out.println("文件下载异常");
}
byte[] b=new byte[1024];
int a=0;
double temp=0.0;
long len=yfile.length();
try {
while((a=fis.read(b))!=-1){
fos.write(b);
temp+=a;
double t=temp/len;
System.out.println(yfile.getName()+"下载了"+df.format(t));
}
} catch (IOException e) {
System.out.println("文件读取异常");
}finally{
try {
fos.close();
fis.close();
} catch (IOException e) {
System.out.println("关闭流异常");
}
}
}
}