java多线程复制文件

package com.xxx.test;

import java.io.*;
import java.util.ArrayList;
import java.util.concurrent.LinkedBlockingQueue;


public class copyfiles {

    public static void main(String[] args) throws IOException, InterruptedException {
        LinkedBlockingQueue<String[]> queue = getFileName("F:\\tidb", "F:\\tidb2");

        ArrayList<Thread> items = new ArrayList();
        for(int i = 0; i < 3; i++){
            Runnable runnable = new CopyFile(queue);
            Thread thread = new Thread(runnable);
            items.add(thread);
        }
        int num = items.size();
        for(int j = 0; j < num; j++){
            items.get(j).start();
        }
    }

    public static LinkedBlockingQueue<String[]> getFileName(String SourcePath, String descPath) throws InterruptedException {
        LinkedBlockingQueue<String[]> queue = new LinkedBlockingQueue<String[]>();
        File fsource = new File(SourcePath);

        if (!fsource.exists()){
            System.out.println(SourcePath + " not exists");
            return queue;
        }
        File fa[] = fsource.listFiles();

        for (int i = 0;i < fa.length; i++){
            File fs = fa[i];
            String[] item = new String[2];
            item[0] = fs.getPath();
            item[1] = descPath + '\\' + fs.getName();
            System.out.println(Arrays.toString(item));
            queue.put(item);
        }

        return queue;
    }


    public static void copyfile(File soure, File dest) throws IOException {
        InputStream input = null;
        OutputStream output = null;
        try {
            input = new FileInputStream(soure);
            output = new FileOutputStream(dest);
            byte[] buf = new byte[1024];
            int bytesread;
            while ((bytesread = input.read(buf)) > 0){
                output.write(buf, 0, bytesread);
            }
        } finally {
            input.close();
            output.close();
        }

    }

}

class CopyFile implements Runnable{
    private LinkedBlockingQueue<String[]> FileQueue;
    public CopyFile(LinkedBlockingQueue<String[]> queue) {
        this.FileQueue = queue;
    }

    public CopyFile() {
    }

    public void run() {
        while (!this.FileQueue.isEmpty()){
            try {
                String[] item = this.FileQueue.take();
                String source = item[0];
                String desc = item[1];
                File fsource = new File(source);
                File fdesc = new File(desc);

                InputStream input = null;
                OutputStream output = null;
                try {
                    input = new FileInputStream(fsource);
                    output = new FileOutputStream(fdesc);
                    byte[] buf = new byte[1024];
                    int bytesread;
                    while ((bytesread = input.read(buf)) > 0){
                        output.write(buf, 0, bytesread);
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                } finally {
                    try {
                        input.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                    try {
                        output.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                    System.out.println(Thread.currentThread().getName() + " " + source + " over!");
                }


            } catch (InterruptedException e) {
                e.printStackTrace();
            }

            try {
                Thread.sleep(1000*5);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

        }
    }
}

java指定运行jar包中的其中一个main方法

java -cp jar包 类名

java -cp E:\IdeaProjects\test\target\test-1.0-SNAPSHOT.jar com.xxx.test.copyfiles
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值