Multi-threaded copy a file

Import java.io.File;
 Import java.io.FileNotFoundException;
 Import java.io.IOException;
 Import java.io.RandomAccessFile;
 Import the java.text.DecimalFormat;
 Import java.util.Date; 

class the FileCopy the implements the Runnable {
     Private File the src ; // source file 
    Private file the tar; // object file 
    Private  int n-; // several portions 
    Private  int NO; // for each part number 
    a Date startDate = new new a Date ();
     public FileCopy(File src, File tar, int n, int no) {
        this.src = src;
        this.tar = tar;
        this.n = n;
        this.no = no;
    }

    @Override
    public void run() {
        try {
            RandomAccessFile rafsrc = new RandomAccessFile(src,"r");
            String fileName = src.getName();
            tar = new File(tar + File.separator + fileName);
            Raftar a RandomAccessFile = new new a RandomAccessFile (the tar, "RW" );
             Long len = src.length ();
             Long size% n-len = len == 0 / n-:? Len / n-+. 1; // number of bytes per portion 
            byte [] B = new new  byte [1024 *. 8]; // read each time the file size 
            int NUM = 0; // number of bytes per read 
            Long Start * NO = size; // read and write from start position 
            rafsrc.seek (Start); 
            raftar.seek (Start); 
            Double SUM = 0; // accumulated number of each read 
            DecimalFormat DF = new newDecimalFormat ( "## 00%." );
             The while ((NUM = rafsrc.read (B)) = -1 && SUM <! Size) { 
                raftar.write (B, 0 , NUM); 
                SUM + = NUM;
                 Double SUM = D / len; 
                System.out.println (fileName + "progress has been copied:" + df.format (D)); 
            } 
            System.out.println (src.getName () + "copy complete!" ); 
            System.out.println ( "copy file" + n + "thread, when used:" + ( new new a Date () the getTime () -. startDate.getTime ())); 
        } the catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e1){
            e1.printStackTrace();
        }
    }
}
/**
 * @Description:ლ【多线程】ლ->复制文件
 * @Param:
 * @Return:
 * @Author: Mr.li
 * @Date: 2020/1/3
 */
public class TestFileCopy {
    public static void main(String[] args){
        File src = new File("/Users/lzl/Desktop/personal_word/a/img_tx.zip");
        File tar = newFile ( "/ the Users / LZL / Desktop / personal_word / B" );
         int n-=. 5 ;
         // several portions replication 
        for ( int I = 0; I <n-; I ++) { // each part number 
            new new the Thread ( new new . the FileCopy (the src, the tar, n-, I)) Start (); 
        } 

    } 
}

Guess you like

Origin www.cnblogs.com/name-lizonglin/p/12144984.html