java 多线程复制文件_java 多线程文件复制

1 importjava.io.File;2 importjava.io.FileInputStream;3 importjava.io.FileNotFoundException;4 importjava.io.IOException;5 importjava.io.InputStream;6 importjava.io.RandomAccessFile;7 importjava.text.DecimalFormat;8

9 public classCopyFile {10

11 private String fileName = "";12 private int buffSize = 1024;13 private long fileSize = 0;14 private int threadNum = 2;15 private String newName = "";16 private String newPath = "";17 private String copyIn = "";18 private boolean showProgress = true;19 private boolean end = false;20 long stime = 0;21 long etime = 0;22 private long copySize = 0;23 private File srcfile = null;24 File newFile = null;25 File filecopyIn = null;26

27 publicCopyFile(String filePath) {28 this.fileName =filePath;29 }30

31 public CopyFile(String filePath, intthreadNum) {32 this.fileName =filePath;33 this.threadNum =threadNum;34 }35

36 public CopyFile(String filePath, int threadNum, intbuffSize) {37 this.fileName =filePath;38 this.buffSize =buffSize;39 this.threadNum =threadNum;40 }41

42 public void to() throwsIOException {43 if (newPath == null || "".equals(newPath)) {44 newPath = newFile(fileName).getParent();45 to(newPath);46 }47 }48

49 public void to(String newPath) throwsIOException {50 if (!newFile(newPath).isDirectory()) {51 throw new IOException("目标必须为目录");52 }53 srcfile = newFile(fileName);54 stime =System.currentTimeMillis();55 this.newName =newFileName(newPath, fileName);56 copyIn = newName + ".copyIN";57 newFile = newFile(newName);58 if(newFile.exists()) {59 newFile.delete();60 }61 filecopyIn = newFile(copyIn);62 if(filecopyIn.exists()) {63 filecopyIn.delete();64 }65

66 InputStream in = newFileInputStream(srcfile);67 fileSize =in.available();68 for (int i = 0; i < threadNum; i++) {69 long s = fileSize / threadNum *i;70 long e = fileSize / threadNum * (i + 1)71 + (i == threadNum - 1 ? fileSize % threadNum : 0);72 newcopyThread(s, e);73 }74 if(showProgress)75 newProgress().start();76

77 }78

79 privateString newFileName(String newPath2, String fileName2) {80 String tmp =srcfile.getName();81 int suffixIndex = tmp.lastIndexOf(".");82 if (suffixIndex > -1) {83 tmp = tmp.substring(0, suffixIndex) + " copy"

84 +tmp.substring(suffixIndex, tmp.length());85 } else{86 tmp += " copy";87 }88 if (!newPath2.endsWith(File.separatorChar + ""))89 newPath2 +=File.separatorChar;90 return newPath2 +tmp;91 }92

93 public longend() {94 long newFileLength = 0;95 if(filecopyIn.renameTo(newFile)) {96 newFileLength =newFile.length();97 } else{98 System.out.println("重命名失败");99 newFileLength =filecopyIn.length();100 }101 long time =System.currentTimeMillis();102 etime =time;103 long haoshi = (etime -stime);104 System.out.println("用时=" + haoshi + "毫秒");105 System.out.println("源文件:" + fileSize + " 复制的文件:" +newFileLength106 + " 丢失:" + (fileSize -newFileLength));107 System.out108 .println("文件:"

109 +Digest.getFileMD5(srcfile).equals(110 Digest.getFileMD5(newFile)));111 end = true;112 returnhaoshi;113 }114

115 class Progress extendsThread {116 public voidrun() {117 DecimalFormat df = new DecimalFormat("0");118 while (!end) {119 long progress = Integer.parseInt(df.format((float) copySize120 / fileSize * 100));121 System.out.println(progress + "%");122 if (progress == 100)123 end();124 try{125 sleep(1000);126 } catch(InterruptedException e) {127 e.printStackTrace();128 }129 }130 }131 }132

133 class copyThread extendsThread {134 private RandomAccessFile in = null;135 private RandomAccessFile out = null;136 private long startOff = 0, endOff = 0;137 private byte[] buff;138

139 public copyThread(long startOff, longendOff) {140 try{141 this.startOff =startOff;142 this.endOff =endOff;143 in = new RandomAccessFile(fileName, "r");144 in.seek(startOff);145 out = new RandomAccessFile(copyIn, "rwd");146 out.seek(startOff);147 start();148 } catch(FileNotFoundException e) {149 e.printStackTrace();150 } catch(IOException e) {151 e.printStackTrace();152 }153 }154

155 public voidrun() {156 try{157 System.out.println("start=" + startOff + " end=" +endOff);158 buff = new byte[buffSize];159 long currentThreadTotal = endOff -startOff;160 long yu = currentThreadTotal %buffSize;161 int off =buffSize;162 long forCount = currentThreadTotal /buffSize163 + (yu > 0 ? 1 : 0);164 for (long i = 0; i < forCount; i++) {165 off = (int) (i == forCount - 1 && yu > 0 ?yu : buffSize);166 int rd = in.read(buff, 0, off);167 if (rd != -1)168 out.write(buff, 0, rd);169 if(showProgress) {170 synchronized (this) {171 copySize +=rd;172 }173 }174 yield();175 }176

177 in.close();178 out.close();179 //end();

180 } catch(Exception e) {181 e.printStackTrace();182 }183 }184 }185

186 public static voidmain(String[] args) {187 String y = "e:/baidu download/贫民窟的百万富翁.rmvb";188 String n = "e:/baidu download/";189 CopyFile cf = new CopyFile(y, 3, 1024 * 1024 * 10);190 try{191 cf.to();192 } catch(IOException e) {193 e.printStackTrace();194 }195

196 }197 }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值