RandomAccessFile实现多线程文件复制
来源:互联网 发布:php 做数据统计图 编辑:程序博客网 时间:2024/06/05 14:38
RandomAccessFile
RandomAccessFile是Java体系中输入/输出流体系中功能最丰富的类之一,拥有强大的API,既支持文件的读,又支持文件的写;支持“随机访问”的方式,即程序可以直接跳转到文件的任意地方来读写数据。利用这个特性可以实现文件的多线程复制,将大文件分成若干段,每一段启动一个线程复制该段的文件,大大提高了效率。
代码实现
import java.io.File;import java.io.FileNotFoundException;import java.io.IOException;import java.io.RandomAccessFile;class CopyFileThread extends Thread{ private RandomAccessFile in; private RandomAccessFile out; private long start; private long end; /** * * @param in 源文件地址 * @param out 目标文件地址 * @param start 分段复制的开始位置 * @param end 分段复制的结束位置 */ public CopyFileThread(String in, String out, long start, long end){ this.start = start; this.end = end; try { this.in = new RandomAccessFile(in, "rw"); this.out = new RandomAccessFile(out, "rw"); } catch (FileNotFoundException e) { e.printStackTrace(); } } public void run(){ try { in.seek(start); out.seek(start); int hasRead = 0; byte[] buff = new byte[1024*1024]; while(start<end && (hasRead = in.read(buff)) != -1){ start += hasRead; out.write(buff, 0, hasRead); } } catch (IOException e) { e.printStackTrace(); }finally{ try { in.close(); out.close(); } catch (IOException e) { e.printStackTrace(); } } }}public class ThreadsCopyFile { /** * * @param sourcePath 源文件路径 * @param targetPath 目标文件路径 * @param ThreadNums 设定的线程数 * @throws IOException */ public void startCopy(String sourcePath, String targetPath, int ThreadNums) throws IOException{ long fileLength = new File(sourcePath).length(); //很有可能文件长度线程不能均分下载,预留一个线程复制最后剩余的部分 long segmentLength = fileLength/(ThreadNums-1); int i; for (i = 0; i < ThreadNums-1; i++) { new CopyFileThread(sourcePath, targetPath, i*segmentLength, (i+1)*segmentLength).start(); } new CopyFileThread(sourcePath, targetPath, i*segmentLength, fileLength).start(); } public static void main(String[] args) throws IOException { //demo中实现的是将e盘中这个目录下的电影复制到d盘下的tttt.rmvb ThreadsCopyFile tcf = new ThreadsCopyFile(); String sourcePath = "E:\\NewFile\\AppData\\Xunlei\\[阳光电影www.ygdy8.com].金刚狼3:殊死一战.HD.720p.中英双字幕.rmvb"; String targetPath = "D:\\tttt.rmvb"; tcf.startCopy(sourcePath, targetPath, 3); }}
总结
总之来说,如果线程越多,buffer区越大,耗费的内存资源也就越多,但是相对的能提升复制的效率。是一种以资源换取效率的方式,因此需要的线程数量的设置,需要考虑到资源的消耗。
阅读全文
1 0
- RandomAccessFile实现多线程文件复制
- RandomAccessFile 多线程的文件复制
- java多线程复制文件,RandomAccessFile类
- 使用RandomAccessFile和多线程来实现开启三个线程去复制一个文件
- RandomAccessFile多线程下载、复制文件、超大文件读写
- 多线程实现文件复制
- JavaIo——运用RandomAccessFile实现文件的多线程下载
- 使用多线程实现文件复制
- 使用RandomAccessFile实现多线程下载
- 使用RandomAccessFile进行文件的复制
- RandomAccessFile实现文件分割、合并
- RandomAccessFile实现文件分割、合并
- Java简单实现多线程复制文件
- 【java编程】IO特殊类之RandomAccessFile实现多线程文件下载
- java用RandomAccessFile实现多线程下载
- RandomAccessFile类实现随机读写文件
- RandomAccessFile实现文件的读入和写入
- RandomAccessFile实现倒序输出文件的内容
- ARM Cortex-A8 的学习笔记(2)——ARM寄存器
- iOS 清除缓存封装
- js jquery 数组的上移下移置顶置底
- 浪漫的平安夜,听一听小白的年终总结(一)之 2015
- android 实现一个或多个角为圆角图片
- RandomAccessFile实现多线程文件复制
- 使用Redis作为LRU缓存
- mysql中 查询表用到的某个字段
- 打印输出100-999之间的水仙花数
- c++中构造函数不能为虚函数的原因
- 备份与恢复
- TCP/IP状态机
- listview嵌套listview
- Linux设备驱动概述