多线程复制自己写

本文介绍了如何使用三个线程来实现单个文件的复制操作,包括主类的设计、线程的创建以及主函数的执行流程。
摘要由CSDN通过智能技术生成

三个线程复制
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("===================================");

    }

}
基于bert实现关系三元组抽取python源码+数据集+项目说明.zip基于bert实现关系三元组抽取python源码+数据集+项目说明.zip基于bert实现关系三元组抽取python源码+数据集+项目说明.zip基于bert实现关系三元组抽取python源码+数据集+项目说明.zip基于bert实现关系三元组抽取python源码+数据集+项目说明.zip 个人大四的毕业设计、课程设计、作业、经导师指导并认可通过的高分设计项目,评审平均分达96.5分。主要针对计算机相关专业的正在做毕设的学生和需要项目实战练习的学习者,也可作为课程设计、期末大作业。 [资源说明] 不懂运行,下载完可以私聊问,可远程教学 该资源内项目源码是个人的毕设或者课设、作业,代码都测试ok,都是运行成功后才上传资源,答辩评审平均分达到96.5分,放心下载使用! 1、该资源内项目代码都经过测试运行成功,功能ok的情况下才上传的,请放心下载使用! 2、本项目适合计算机相关专业(如计科、人工智能、通信工程、自动化、电子信息等)的在校学生、老师或者企业员工下载学习,也适合小白学习进阶,当然也可作为毕设项目、课程设计、作业、项目初期立项演示等。 3、如果基础还行,也可在此代码基础上进行修改,以实现其他功能,也可用于毕设、课设、作业等。 下载后请首先打开README.md文件(如有),供学习参考。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值