java多线程复制文件

2021/9/28 14:10:53

本文主要是介绍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


这篇关于java多线程复制文件的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程