`
ChangMing0810
  • 浏览: 4191 次
  • 性别: Icon_minigender_2
  • 来自: 长沙
文章分类
社区版块
存档分类
最新评论

多线程复制文件

阅读更多
package 多线程复制文件2;

import java.io.File;

import 多线程复制文件2.Copy.ThreadCopyFile;

public class Main {
	public static void main(String[] args) {
		// 获取文件的长度 把文件分成3块
		long filelength = new File(ThreadCopyFile.path).length();

		long size = filelength / 3;

		// 定义3个线程
		// for (int i = 0; i < 3; i++) {
		// new ThreadCopyFile(i * size, (i + 1) * size).start();
		// }

		ThreadCopyFile thread0 = new ThreadCopyFile(0, size);
		thread0.start();
		ThreadCopyFile thread1 = new ThreadCopyFile(size, 2 * size);
		thread1.start();
		ThreadCopyFile thread2 = new ThreadCopyFile(2 * size, filelength);
		thread2.start();
	}

}

 

package 多线程复制文件2;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.RandomAccessFile;

public class Copy {
	public static class ThreadCopyFile extends Thread {
		static String path = "C:\\Users\\CM\\Desktop\\a.avi";
		String topath = "C:\\Users\\CM\\Desktop\\a_附件.avi";
		private long from; // 复制起始位置 
		private long to; // 复制结束位置 

		public ThreadCopyFile(long from, long to) {
			this.from = from;
			this.to = to;
		}

		public void run() {
			try {
				InputStream in = new FileInputStream(path); //用字节流复制文件
				RandomAccessFile out = new RandomAccessFile(topath, "rw");
				in.skip(from); // 跳一定的字节后再开始读取
				out.seek(from); // 从一定字节后开始写入
				long sum = 0; // 统计读取了多少个字节数
				byte[] b = new byte[1024 * 1024];
				int len = 0;
				//读取的字节数必须有数量限制 限制小于 to 和from 的差
				while ((len = in.read(b)) != -1 && sum <= (to - from)) {
					out.write(b, 0, len);
					sum += len;
				}
				in.close();
				out.close();
				
				System.out.println(Thread.currentThread().getName() + " 复制完成");
			} catch (Exception e) {
				e.printStackTrace();
			} 
		}

		
	}

}

 

package 多线程复制文件;


import java.io.RandomAccessFile;
import java.nio.channels.FileChannel;
import java.nio.channels.FileLock;

public class CopyThread extends Thread{

	private String srcPath;
	private String destPath;
	private int start, end;

	public CopyThread(String srcPath, String destPath, int start, int end) {
		//要复制的源文件路径
		this.srcPath = srcPath;
		//复制到的文件路径
		this.destPath = destPath;
		//复制起始位置
		this.start = start;
		//复制结束位置
		this.end = end;
	}

	public void run() {
		try {
			RandomAccessFile in = new RandomAccessFile(srcPath, "r");
			RandomAccessFile out = new RandomAccessFile(destPath, "rw");
			
			// 将输入跳转到指定位置
			in.seek(start);
			// 从指定位置开始写
			out.seek(start);
			
			FileChannel inChannel = in.getChannel();
			FileChannel outChannel = out.getChannel();
			
			//锁住需要操作的区域
			FileLock lock = outChannel.lock(start,  (end-start), false);
	
//			inChannel.transferTo(position, count, outChannel);  或:
			outChannel.transferFrom(inChannel, start, (end-start));

			//释放锁
			lock.release();
			out.close();
			in.close();

		} catch (Exception e) {
			e.printStackTrace();
		}

	}

}

 

package 多线程复制文件;

import java.io.File;

public class Main {

	public static void main(String[] args) {
		//要复制的源文件路径
		String srcPath = "C:\\Users\\CM\\Desktop\\a.avi";
		//
		String destPath = "C:\\Users\\CM\\Desktop\\a_复件.avi";

		// 获得源文件长度
		File f = new File(srcPath);
		long len = f.length();

		int count = 3;// 需要的线程数
		//强制转换成int类型
		int oneNum = (int) (len / count);

		for (int i = 0; i < count - 1; i++) {
			CopyThread ct = new CopyThread(srcPath, destPath, oneNum * i,oneNum * (i + 1));
			ct.start();
		}
		CopyThread ct = new CopyThread(srcPath, destPath, oneNum * (count-1),(int)len);
		ct.start();
	}
}

 

 多线程复制文件的两个方法:

多线程复制文件2 和 多线程复制文件

1.多线程复制文件2是用字节流的方式

利用byte[] b = new byte[1024 * 1024]加快传输速度;

 

2.多线程复制文件用FileChannel,文件复制效率高;

 

 

2
1
分享到:
评论
5 楼 ChangMing0810 2016-08-24  
cywhoyi 写道
如果是文件内容是中文,按照这样写法是半个字符;
如果是文件是图片、音频,怎么办

图片,音频,中文文档文件也是可以用这个方法复制的,我已经实验过了,没有影响
4 楼 cywhoyi 2016-08-24  
如果是文件内容是中文,按照这样写法是半个字符;
如果是文件是图片、音频,怎么办
3 楼 tieye 2016-08-24  
wangrui444888 写道
博主写的很不错,挺有帮助的,已经关注你了,以后要多更新啊,美女加油!!

这马屁拍的 拍出了新高度
2 楼 Sunflower-13 2016-08-24  
美女加油哦
1 楼 wangrui444888 2016-08-24  
博主写的很不错,挺有帮助的,已经关注你了,以后要多更新啊,美女加油!!

相关推荐

Global site tag (gtag.js) - Google Analytics