利用多线程拷贝内存巨大的文件并显示进度

效果图:

利用多线程拷贝内存巨大的文件并显示进度_第1张图片

多线程运行拷贝文件代码:

@Override
	public void run() {
		try(RandomAccessFile read = new RandomAccessFile(source,"r");
			RandomAccessFile write = new RandomAccessFile(target,"rw");
		){	
			read.seek(start);
			write.seek(start);
			//统计一条线程读取的字节数
			int count = 0;
			byte[] b = new byte[1024];
			int len = 0;
			
			System.out.println(getName()+"开始拷贝"+start);
			while((len = read.read(b)) != -1) {
				write.write(b,0,len);
				count += len;
				//如果目前读取的总字节数超过了每一段的长度则停止读取
				//如果是最后一条线程,则不停止执行
				position += len;
				if(count >= (end - start) && !"t3".equals(getName())) {
					break;
				}
			}
			System.out.println(getName()+"拷贝完成,拷贝长度:"+count);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e1) {
			e1.printStackTrace();
		}
	}

记录拷贝进度代码:

		new Thread() {
			@Override
			public void run() {
				
				while(true) {
					DecimalFormat fmt = new DecimalFormat("00.00%");
					double per = position*1.0 / source.length();
					if(per > 1) {
						System.out.println("当前进度:100.00%");
						System.out.println("程序拷贝结束!");
						break;
					}
					String percent = fmt.format(per);
					System.out.println("当前进度:"+percent);
					super.run();
				}
				try {
					sleep(1000);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
		}.start();
		

总代码:

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.text.DecimalFormat;

/**
 * 	多线程拷贝大文件
 * @author 洲小徐
 *
 */
public class FileSplitCopy extends Thread{
	
	/**源文件*/
	private File source;
	/**目标文件*/
	private File target;
	/**单个线程开始读取位置*/
	private long start;
	/**单个线程结束读取位置*/
	private long end;
	/**标记已完成多少字节*/
	private static long position;
	
	public FileSplitCopy(File source, File target, long start, long end) {
		super();
		this.source = source;
		this.target = target;
		this.start = start;
		this.end = end;
	}
	
	public FileSplitCopy() {
		
	}
	
	@Override
	public void run() {
		try(RandomAccessFile read = new RandomAccessFile(source,"r");
			RandomAccessFile write = new RandomAccessFile(target,"rw");
		){	
			read.seek(start);
			write.seek(start);
			//统计一条线程读取的字节数
			int count = 0;
			byte[] b = new byte[1024];
			int len = 0;
			
			System.out.println(getName()+"开始拷贝"+start);
			while((len = read.read(b)) != -1) {
				write.write(b,0,len);
				count += len;
				//如果目前读取的总字节数超过了每一段的长度则停止读取
				//如果是最后一条线程,则不停止执行
				position += len;
				if(count >= (end - start) && !"t3".equals(getName())) {
					break;
				}
			}
			System.out.println(getName()+"拷贝完成,拷贝长度:"+count);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e1) {
			e1.printStackTrace();
		}
		
		
	}
	
	public static void main(String[] args) {
		
		File source = new File("D:\\Temple\\阿里云.mp4");
		File target = new File("D:\\Temple\\目标目录",source.getName());
		//平均获取每一段长度
		long item = source.length() / 4;
		for (int i = 0; i < 4; i++) {
			FileSplitCopy fsc = new FileSplitCopy(source,target,item*i,item*(i+1));
			fsc.setName("t"+i);
			fsc.start();
		}
		
		new Thread() {
			@Override
			public void run() {
				
				while(true) {
					DecimalFormat fmt = new DecimalFormat("00.00%");
					double per = position*1.0 / source.length();
					if(per > 1) {
						System.out.println("当前进度:100.00%");
						System.out.println("程序拷贝结束!");
						break;
					}
					String percent = fmt.format(per);
					System.out.println("当前进度:"+percent);
					super.run();
				}
				try {
					sleep(1000);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
		}.start();;
		
	}
	
}

你可能感兴趣的:(java,eclipse)