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