getFilePointer
方法和由設置
seek
方法。
//Test01前端
package com.wxg.download_threads; import java.io.File; import java.util.Scanner; public class Test01 { public static void main(String[] args) throws Exception { File sourceFile=new File("性感荷官在線發牌.avi"); File targetFile=new File("copy.avi"); Scanner scan = new Scanner(System.in); System.out.println("請輸入須要啓動的線程數量(最多8個)"); int copyNum=scan.nextInt(); scan.close(); if(copyNum>8||copyNum<=0){ System.out.println("輸入錯誤"); return; } long copySize=sourceFile.length()/copyNum;//計算前copyNum-1個線程拷貝文件的分段大小 int i; for(i=0;i<copyNum-1;i++){ new DownloadUtilThreads(sourceFile, targetFile, copySize, copySize*i).start(); } new DownloadUtilThreads(sourceFile, targetFile, copySize+(sourceFile.length()%copyNum), copySize*(i+1)).start(); } }
//拷貝線程:DownloadUtilThreadsjava
package com.wxg.download_threads; import java.io.File; import java.io.FileNotFoundException; import java.io.RandomAccessFile; public class DownloadUtilThreads extends Thread { private long copySize; private long point; private RandomAccessFile r; private RandomAccessFile w; private File logFile; public DownloadUtilThreads(File sourceFile,File targetFile,long copySize,long point) throws FileNotFoundException { this.copySize=copySize; this.point=point; r=new RandomAccessFile(sourceFile, "r"); w=new RandomAccessFile(targetFile, "rw"); } @Override public void run() { try { //建立日誌操做對象 logFile=new File(Thread.currentThread().getName()+"_download.log"); LogOpreator logOpreator = new LogOpreator(logFile); //首次啓動下載 if(logFile.length()==0){ logOpreator.write(point,false); } //讀取日誌文件取出point,isFinish long startIndex = logOpreator.readPoint(); boolean isFinish = logOpreator.readIsFinish(); System.out.println(startIndex+"---"+isFinish); //判斷是否已經下載完成 if(isFinish){ return; } //設置指針偏移 r.seek(startIndex); w.seek(startIndex); //拷貝 byte[] b=new byte[8192]; int len; while((len=r.read(b))!=-1){ w.write(b, 0, len); startIndex+=len; logOpreator.write(startIndex,false); //判斷拷貝是否完成 if(startIndex>=copySize){ logOpreator.write(startIndex,true); System.out.println(startIndex+"---"+isFinish); break; } } } catch (Exception e) { e.printStackTrace(); } } }
//實體存儲對象:DownloadLog(其中point用於存儲文件指針,isFinish用於存儲文件是否拷貝完)api
package com.wxg.download_threads; import java.io.Serializable; public class DownloadLog implements Serializable { /** * */ private static final long serialVersionUID = 1L; private long point; private boolean isFinish=false; public DownloadLog() { } public DownloadLog(long point,boolean isFinish) { this.point = point; this.isFinish=isFinish; } public long getPoint() { return point; } public void setPoint(long point) { this.point = point; } @Override public String toString() { return "DownloadLog [point=" + point + "]"; } public boolean isFinish() { return isFinish; } public void setFinish(boolean isFinsh) { this.isFinish = isFinsh; } }
//日誌讀寫操做:LogOpreator數組
package com.wxg.download_threads; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; public class LogOpreator { private DownloadLog d; private ObjectInputStream obi; private ObjectOutputStream obo; private File file; public LogOpreator(File file) { this.file = file; } //寫入對象到文件 public void write(long point,boolean isFinish){ d=new DownloadLog(point,isFinish); try { obo=new ObjectOutputStream(new FileOutputStream(file)); obo.writeObject(d); obo.close(); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } //取出point public long readPoint(){ try { obi=new ObjectInputStream(new FileInputStream(file)); Object logOb = obi.readObject(); DownloadLog log=(DownloadLog)logOb; obi.close(); return log.getPoint(); } catch (Exception e) { e.printStackTrace(); } return 0; } public boolean readIsFinish(){ try { obi=new ObjectInputStream(new FileInputStream(file)); Object logOb = obi.readObject(); DownloadLog log=(DownloadLog)logOb; obi.close(); return log.isFinish(); } catch (Exception e) { e.printStackTrace(); } return false; } }