AccessRandomFile multi-thread download file, accessrandomfile

Source: Internet
Author: User

AccessRandomFile multi-thread download file, accessrandomfile

 

Write a tool class

Package com. pb. thread. demo; import java. io. file; import java. io. fileNotFoundException; import java. io. IOException; import java. io. randomAccessFile;/*** multithread file copying tool class ** @ author denny **/public class MutiCopyFileUtil {private String src; // source file private String dst; // private int threadSize of the target file; // how many threads are enabled to copy the source file/*** constructor containing three parameters ** @ param src * Source File * @ param dst * target file * @ param threadSize * Enabled how many threads are used to copy source files? */Public MutiCopyFileUtil (String src, String dst, int threadSize) {super (); this. src = src; this. dst = dst; this. threadSize = threadSize;}/*** method of copying a File */public void copyFile () {file File = new File (src ); // File object of the source file // obtain the size of the source File in bytes long fsize = file. length (); // calculate the number of bytes downloaded by each thread. long block = fsize % threadSize = 0? Fsize/threadSize: fsize/threadSize + 1; // enable loop call for (int threadId = 0; threadId <threadSize; threadId ++) {// enable multiple threads to copy files, new DownloadThread (fsize, block, threadId ). start () ;}}/***** @ author denny **/private class DownloadThread extends Thread {private long fsize; // file size private long block; // The number of bytes downloaded by each thread private int threadId; // The thread ID, starting from 0 private int buffSize = 1024*1024; // the buffer size Public DownloadThread (long fsize, long block, int threadId) {super (); this. fsize = fsize; this. block = block; this. threadId = threadId;}/*** override the run Method */public void run () {try {// declare the IO class for randomly Accessing file objects, access the file RandomAccessFile reader = new RandomAccessFile (src, "r") in read-only mode; // randomly access the file object RandomAccessFile writer = new RandomAccessFile (dst, "rw "); // the start position of each thread to download long startPos = threadId * block; // determine the download position of each thread Long endPos = startPos + block> fsize? Fsize: startPos + block;/** determine whether the download end location is greater than the maximum position of the file. * If yes, it is the maximum position of the file, * No, it is the start position + the size of the byte to be downloaded * // you can specify the cursor position as reader. seek (startPos); // set the writer of the target file. seek (startPos); // set the buffer byte [] buf = new byte [buffSize]; while (startPos <endPos) {int len = 0; // determine whether it is to the end position if (startPos + buffSize <endPos) {// read the file to the buffer, read the file full len = reader. read (buf);} else {// write the remaining data that is not in the buffer zone to len = reader. read (buf, 0, (int) (endPos-startPos);} // change the starting position startPos + = len; /* write the target file * write the buffer file to the target file */writer. write (buf, 0, len); System. out. println ("Thread" + (threadId + 1) + "downloaded:" + len);} reader. close (); writer. close (); System. out. println ("Thread" + (threadId + 1) + "download completed");} catch (IOException e) {e. printStackTrace ();}}}}

Test class

package com.pb.thread.demo;import java.io.File;/** *  * @author denny * */public class MutiCopyFileDemo {    public static void main(String[] args) {        /*         * /home/denny/Downloads         * android-studio-ide-141.2135290-linux.zip         */        String src="/home/denny/Downloads/android-studio-ide-141.2135290-linux.zip";        String dst="/home/denny/Downloads/a.zip";        MutiCopyFileUtil mcf=new MutiCopyFileUtil(src,dst,10);        mcf.copyFile();    }}

 

Result:

Thread 9 download: 1048576 thread 6 download: 1048576 thread 4 download: 1048576 thread 3 download: 1048576 thread 1 download: 1048576 thread 8 download: 1048576 thread 2 download: 1048576 thread 5 download: 1048576 thread 7 download: 1048576 thread 10 download: 1048576 thread 9 download: 1048576 thread 1 download: 1048576 thread 7 download: 1048576 thread 3 download: 1048576 thread 6 download: 1048576 thread 4 download: 1048576 thread 2 download: 1048576 thread 10 download: 1048576 thread 3 download: 1048576 thread 8 download: 1048576 thread 6 download: 1048576 thread 5 download: 1048576 thread 1 download: 1048576 thread 4 download: 1048576 thread 7 download: 1048576 thread 9 download: 1048576 thread 2 download: 1048576 thread 4 download: 1048576 thread 6 download: 1048576 thread 8 download: 1048576 thread 7 download: 1048576 thread 9 download: 1048576 thread 1 download: 1048576 thread 2 download: 1048576 thread 5 download: 1048576 thread 3 download: 1048576 thread 7 download: 1048576 thread 10 download: 1048576 thread 2 download: 1048576 thread 5 download: 1048576 thread 2 download: 1048576 thread 9 download: 1048576 thread 4 download: 1048576 thread 10 download: 1048576 thread 5 download: 1048576 thread 10 download: 1048576 thread 9 download: 1048576 thread 4 download: 1048576 thread 2 download: 1048576 thread 5 download: 1048576 thread 3 download: 1048576 thread 8 download: 1048576 thread 10 download: 1048576 thread 9 download: 1048576 thread 1 downloaded: 1048576 thread 7 downloaded: 1048576 thread 6 downloaded: 1048576 thread 10 downloaded: 1048576 thread 4 downloaded: 1048576 thread 3 downloaded: 1048576 thread 9 download: 1048576 thread 1 download: 1048576 thread 8 download: 1048576 thread 6 download: 1048576 thread 4 download: 1048576 thread 10 download: 1048576 thread 5 download: 1048576 thread 7 download: 1048576 thread 3 download: 1048576 thread 1 download: 1048576 thread 9 download: 1048576 thread 2 download: 1048576 thread 5 download: 1048576 thread 4 download: 1048576 thread 10 download: 1048576 thread 1 download: 1048576 thread 2 download: 1048576 thread 1 download: thread 1 download: 1048576 thread 7 download: 1048576 thread 8 download: 1048576 thread 6 download: 1048576 thread 9 download: 1048576 thread 3 download: 1048576 thread 1 download: 1048576 thread 2 download: 1048576 thread 5 download: 1048576 thread 10 download: 1048576 thread 4 download: 1048576 thread 7 download: 1048576 thread 10 download: 1048576 thread 3 download: 1048576 thread 7 download: 1048576 thread 6 download: 1048576 thread 8 download: 1048576 thread 9 download: 1048576 thread 10 download: 1048576 thread 6 download: 1048576 thread 1 download: 1048576 thread 5 download: 1048576 thread 4 download: 1048576 thread 2 download: 1048576 thread 9 download: 1048576 thread 7 download: 1048576 thread 6 download: 1048576 thread 8 download: 1048576 thread 2 download: 1048576 thread 7 download: 1048576 thread 3 download: 1048576 thread 9 download: 1048576 thread 6 download: 1048576 thread 10 download: 1048576 thread 5 download: 1048576 thread 4 download: 1048576 thread 1 download: 1048576 thread 8 download: 1048576 thread 3 download: 1048576 thread 6 download: 1048576 thread 9 download: 1048576 thread 4 download: 1048576 thread 7 download: 1048576 thread 2 download: 1048576 thread 5 download: 1048576 thread 1 download: 1048576 thread 10 download: 1048576 thread 6 download: 1048576 thread 10 download: thread 10 download: 1048576 thread 9 download: 1048576 thread 1 download: 1048576 thread 8 download: 1048576 thread 5 download: 1048576 thread 3 download: 1048576 thread 4 download: 1048576 thread 7 download: 1048576 thread 2 download: 1048576 thread 10 download: 1048576 thread 6 download: 1048576 thread 9 download: 1048576 thread 5 download: 1048576 thread 10 download: 1048576 thread 8 download: 1048576 thread 7 download: 1048576 thread 1 download: 1048576 thread 2 download: 1048576 thread 4 download: 1048576 thread 6 download: 1048576 thread 10 download: 1048576 thread 3 download: 1048576 thread 5 download: 1048576 thread 9 download: 1048576 thread 2 download: 1048576 thread 10 download: 1048576 thread 6 download: 1048576 thread 7 download: 1048576 thread 8 download: 1048576 thread 5 download: 1048576 thread 9 download: 1048576 thread 10 download: 1048576 thread 4 download: 1048576 thread 3 download: 1048576 thread 6 download: 1048576 thread 8 download: 1048576 thread 7 download: 1048576 thread 10 download: 1048576 thread 5 download: 1048576 thread 2 download: 1048576 thread 3 download: 1048576 thread 5 download: 1048576 thread 7 download: 1048576 thread 1 downloaded: 1048576 thread 2 downloaded: 104 thread 8 downloaded: 8576 thread 9 downloaded: 1048576 thread 10 downloaded: 1048576 thread 4 downloaded: 1048576 thread 6 download: 1048576 thread 7 download: 1048576 thread 3 download: 1048576 thread 2 download: 1048576 thread 5 download: 1048576 thread 8 download: 1048576 thread 4 download: 1048576 thread 10 download: 1048576 thread 6 download: 1048576 thread 1 download: 1048576 thread 6 download: 1048576 thread 5 download: thread 5 download: 1048576 thread 9 download: 1048576 thread 7 download: 1048576 thread 6 download: 1048576 thread 3 download: 1048576 thread 7 download: 1048576 thread 10 download: 1048576 thread 8 download: 1048576 thread 5 download: 1048576 thread 7 download: 1048576 thread 1 download: 1048576 thread 2 download: 1048576 thread 6 download: 1048576 thread 9 download: 1048576 thread 5 download: 1048576 thread 4 download: 1048576 thread 8 download: 1048576 thread 3 download: 1048576 thread 8 download: thread 8 download: 1048576 thread 7 download: 1048576 thread 7 download: 1048576 thread 7 download: 1048576 thread 7 download: 1048576 thread 6 download: 1048576 thread 3 download: 1048576 thread 8 download: 1048576 thread 5 download: 1048576 thread 9 download: 1048576 thread 10 download: 1048576 thread 2 download: 1048576 thread 4 download: 1048576 thread 2 download: 1048576 thread 1 download: 1048576 thread 8 download: 1048576 thread 5 download: 1048576 thread 6 download: 1048576 thread 10 download: 1048576 thread 9 download: 1048576 thread 7 download: 1048576 thread 2 download: 1048576 thread 4 download: 1048576 thread 3 download: 1048576 thread 2 download: 1048576 thread 5 download: 1048576 thread 8 download: 1048576 thread 8 download: 1048576 thread 6 download: 1048576 thread 3 download: 1048576 thread 1 download: 1048576 thread 10 download: 1048576 thread 4 download: 1048576 thread 5 download: 1048576 thread 2 download: 1048576 thread 7 download: 1048576 thread 6 download: 1048576 thread 9 download: 1048576 thread 4 download: 1048576 thread 2 download: 1048576 thread 5 download: 1048576 thread 8 download: 1048576 thread 6 download: 1048576 thread 10 download: 1048576 thread 7 download: 1048576 thread 6 download: 1048576 thread 6 download: 1048576 thread 6 download: 1048576 thread 6 download: thread 6 download: thread 6 download: 1048576 thread 6 download: 1048576 thread 6 download: 1048576 thread 6 download: 596259 thread 5 download: 1048576 thread 10 download: 1048576 thread 9 download: 1048576 thread 10 download: 1048576 thread 1 download: 1048576 thread 3 download: 1048576 thread 2 download: 1048576 thread 7 download: 1048576 thread 6 download completed thread 9 download: 1048576 thread 2 download: 1048576 thread 7 download: 1048576 thread 10 download: 1048576 thread 4 download: 1048576 thread 5 download: 1048576 thread 2 download: 1048576 thread 8 download: 1048576 thread 9 download: 1048576 thread 5 download: 1048576 thread 7 download: 1048576 thread 1 download: 1048576 thread 2 download: 1048576 thread 3 download: 1048576 thread 2 download: 1048576 thread 3 download: 1048576 thread 1 download: 1048576 thread 10 download: 1048576 thread 7 download: 1048576 thread 5 download: 1048576 thread 8 download: 1048576 thread 2 download: 1048576 thread 7 download: 596259 thread 7 download completed thread 10 download: 1048576 thread 4 download: 1048576 thread 9 download: 1048576 thread 3 download: 1048576 thread 1 download: 1048576 thread 10 download: 596259 thread 10 download completed thread 2 download: 1048576 thread 4 download: 1048576 thread 9 download: 1048576 thread 8 download: 1048576 thread 5 download: 1048576 thread 2 download: 596259 thread 2 download completed thread 3 download: 1048576 thread 1 download: 1048576 thread 3 download: 1048576 thread 4 download: 1048576 thread 1 download: 1048576 thread 9 download: 1048576 thread 8 download: 1048576 thread 3 download: 1048576 thread 4 download: 1048576 thread 5 download: 1048576 thread 8 download: 1048576 thread 9 download: 1048576 thread 3 download: 1048576 thread 5 download: 596259 thread 9 download: 1048576 thread 1 download: 1048576 thread 5 download completed thread 4 download: 1048576 thread 8 download: 1048576 thread 3 download: 1048576 thread 9 download: 1048576 thread 3 download: 1048576 thread 8 download: 1048576 thread 4 download: 1048576 thread 9 download: 596259 thread 9 download completed thread 1 download: 1048576 thread 4 download: 1048576 thread 3 download: 1048576 thread 8 download: 1048576 thread 1 download: 1048576 thread 4 download: 1048576 thread 8 download: 1048576 thread 3 download: 1048576 thread 1 download: 1048576 thread 4 download: 1048576 thread 3 download: 596259 thread 3 download completed thread 4 download: 596259 thread 1 download: 1 048576 thread 4 download completed thread 8 download: 1048576 thread 8 download: 596259 thread 8 download completed thread 1 download: 1048576 thread 1 download: 1048576 thread 1 download: thread 1 download: 596259 thread 1 download complete

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.