首页 > 程序开发 > 软件开发 > 其他 > 正文
多线程复制
2017-03-20       个评论    来源:qq_35286826的博客  
收藏    我要投稿

多线程复制:三个线程复制,希望下面的文章对大家有所帮助。
1、主类

package RandomFileCopy;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.RandomAccessFile;

/*/1.读取文件长度
 * 2、分三个线程来copy,用seek来确定读写开始和结束为止
 * 3、用while,if控制读取和读写
 * 4、
 * 5、将文件
 */

public class CopyBig {
    // socpath为要复制文件目录地址,topath为要复制到另一目录的地址,x为线程总数,y为线程序号
    public static void ReaderW(String socpath, String topath, int x, int y) {
        RandomAccessFile raf = null;
        RandomAccessFile rtf = null;
        try {
            raf = new RandomAccessFile(socpath, "rw"); // 以读写的方式打开一个RandomAccessFile,raf为要读取得文件
            rtf = new RandomAccessFile(topath, "rw");// 以读写的方式打开一个RandomAccessFile,rtf为要写入的文件
            // 获取截断长度
            System.out.println("总长度:" + (raf.length()));
            /*
             * 本段注解,x为总数,len为前面x-1次线程要复制的长度,当线程y=x时,长度可能会比前面的稍大
             * 但开始是从0开始的,所以开始的只要是平均长度*y-1就可以;结束要分y=x和要y!=x两种情况
             */
            long len = (raf.length()) / x;
            long begin = len * (y - 1);
            long end;
            if (!(y == x)) {
                end = len * y;
                System.out.println("线程" + y+"   开始" + begin);
                System.out.println("线程" + y+"   结束" + end + "\n");
            } else {

                end = len * y + ((raf.length()) % x);
                System.out.println("線程" + y+"   開始" + begin);
                System.out.println("線程" + y+"   結束" + end + "\n");
            }

            // 设定读取和写入开始指针位置
            raf.seek(begin);
            rtf.seek(begin);
            byte[] b = new byte[1024 * 1024];
            while (begin < end) {
                int length = 0;
                // 读取
                if (((begin + 1024 * 1024) < end)) {
                    length = raf.read(b);

                } else {
                    length = raf.read(b, 0, (int) (end - begin));
                    System.out.println("线程" + y + ":" + raf.length());
                    //rtf.write(b, 0, length);
                }
                rtf.write(b, 0, length);
                begin += length;

            }

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (rtf != null)
                    rtf.close();
            } catch (IOException e) {
                e.printStackTrace();
                rtf = null;
            }
            try {
                if (raf != null)
                    raf.close();
            } catch (IOException e) {
                e.printStackTrace();
                raf = null;
            }
        }

    }

    public static void CreateFile(String path) {
        File f = new File(path);
        if (!f.exists()) {
            File parent = f.getParentFile();
            if (!parent.exists()) {
                parent.mkdirs();
            }

            try {
                f.createNewFile();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

        }

    }

    // 单线程
    public static void download(String socpath, String topath)
            throws IOException {
        File in = new File(socpath);
        long begin = System.currentTimeMillis();
        BufferedInputStream filein = new BufferedInputStream(
                new FileInputStream(socpath));
        BufferedOutputStream fileout = new BufferedOutputStream(
                new FileOutputStream(topath));
        byte buffer[] = new byte[1024 * 1024];
        int length = 0;
        while ((length = filein.read(buffer)) != -1) {
            fileout.write(buffer, 0, length);
            File on = new File(topath);
            // System.out.println("原来长度"+in.length());
            System.out.println("存入长度" + (on.length() / (1024 * 1024)) + "M");
            System.out.println("Download "
                    + (((float) on.length()) / ((float) in.length())));
        }

        fileout.close();
        filein.close();
        long end = System.currentTimeMillis();
        System.out.println("Download successfully..." + (end - begin) + "ms");
    }

}

// public static void main(String args[]) {
// CreateFile("C:\\360安全浏览器下载\\123.txt");
// ReaderW("C:\\Users\\EDUASK\\Desktop\\12345.txt","C:\\360安全浏览器下载\\123.txt",
// 3,1);
// ReaderW("C:\\Users\\EDUASK\\Desktop\\12345.txt","C:\\360安全浏览器下载\\123.txt",
// 3,2);
// ReaderW("C:\\Users\\EDUASK\\Desktop\\12345.txt","C:\\360安全浏览器下载\\123.txt",
// 3,3);
//
// }

// }

2线程

package RandomFileCopy;

public class Thread1 extends Thread{
    private CopyBig c;
    private String socpath;//原始文件路径
    private String topath;//复制路径
    private int y;
    public Thread1(CopyBig c,String socpath, String topath,int y){
        this.c=c;
        this.socpath=socpath;
        this.topath=topath;
        this.y=y;

    }
    public void run(){

        long begin=System.currentTimeMillis();
        c.ReaderW(socpath,topath, 3,y);
        long end=System.currentTimeMillis();
        System.out.println((end-begin)+"ms");
        System.out.println("===================================");
    }System.currentTimeMillis();

}
package RandomFileCopy;

public class Thread2 extends Thread{
    private CopyBig c;
    private String socpath;//原始文件路径
    private String topath;//复制路径
    private int y;
    public Thread2(CopyBig c,String socpath, String topath,int y){
        this.c=c;
        this.socpath=socpath;
        this.topath=topath;
        this.y=y;

    }
    public void run(){

        long begin=System.currentTimeMillis();
        c.ReaderW(socpath,topath, 3,y);
        long end=System.currentTimeMillis();
        System.out.println((end-begin)+"ms");
        System.out.println("===================================");
    }

}
package RandomFileCopy;

public class Thread3 extends Thread{
    private CopyBig c;
    private String socpath;//原始文件路径
    private String topath;//复制路径
    private int y;
    public Thread3(CopyBig c,String socpath, String topath,int y){
        this.c=c;
        this.socpath=socpath;
        this.topath=topath;
        this.y=y;

    }
    public void run(){

        long begin=System.currentTimeMillis();
        c.ReaderW(socpath,topath, 3,y);
        long end=System.currentTimeMillis();
        System.out.println((end-begin)+"ms");
        System.out.println("===================================");
    }

}

3主函数

package RandomFileCopy;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.RandomAccessFile;
import java.util.Scanner;

public class CopyTest {
    public static void main(String args[]) throws IOException {

        System.out.println("请输入原始地址");
        BufferedReader buf = new BufferedReader(new InputStreamReader(System.in));
        String socpath = buf.readLine();


        System.out.println("请输入复制地址");
        BufferedReader buff = new BufferedReader(new InputStreamReader(System.in));
        String topath = buff.readLine();
        buf.close();
        /*
         * 原始文件路径 String socpath="D:\\源文件\\CAD2010 64位.zip"; 复制路径 String
         * topath="C:\\360安全浏览器下载\\计算机\\cad123.zip";
         * 
         * //在目标目录不存在要复制文件,创建文件,
         */
        CopyBig cc = new CopyBig();
        cc.CreateFile(topath);
        // create("D:\\源文件\\CAD2010 64位.zip","C:\\360安全浏览器下载\\计算机\\cad123.zip");
        // 启动线程
        new Thread1(cc, socpath, topath, 1).start();
        new Thread2(cc, socpath, topath, 2).start();
        new Thread3(cc, socpath, topath, 3).start();

        // new Single(cc).start();

    }
    // public static void create(String socpath, String topath) throws
    // IOException{
    // RandomAccessFile raf = new RandomAccessFile(socpath, "rw");
    // RandomAccessFile rtf = new RandomAccessFile(topath, "rw");
    // rtf.setLength(raf.length());
    // }
}

单个文件复制

package RandomFileCopy;

import java.io.IOException;

public class Single extends Thread{

    private CopyBig c;
    public Single(CopyBig c){
        this.c=c;

    }
    public void run(){
        long begin=System.currentTimeMillis();
        try {
            c.download("D:\\源文件\\CAD2010 64位.zip","C:\\360安全浏览器下载\\计算机\\cad1.zip");
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        long end=System.currentTimeMillis();
        System.out.println((end-begin)+"ms");
        System.out.println("===================================");

    }

}
点击复制链接 与好友分享!回本站首页
上一篇:算法提高 棋盘多项式
下一篇:wampserver可以打开localhost但是无法打开项目的解决方法
相关文章
图文推荐
文章
推荐
点击排行

关于我们 | 联系我们 | 广告服务 | 投资合作 | 版权申明 | 在线帮助 | 网站地图 | 作品发布 | Vip技术培训
版权所有: 红黑联盟--致力于做实用的IT技术学习网站