Skip to content

Instantly share code, notes, and snippets.

@rokon12
Last active March 23, 2017 22:40
Show Gist options
  • Save rokon12/e89aeeceb94a2d15d453733532e4af52 to your computer and use it in GitHub Desktop.
Save rokon12/e89aeeceb94a2d15d453733532e4af52 to your computer and use it in GitHub Desktop.
import java.io.File;
import java.util.ArrayList;
import java.util.List;
public class CopyApp {
public void copyFiles(String from, String to) {
List<File> files = listFile(from);
List<Thread> threads = new ArrayList<>();
for (File file : files) {
String name = file.getName();
String copyTo = to + File.separator + name;
CopyThread copyThread = new CopyThread(file.getAbsolutePath(), copyTo);
copyThread.start();
threads.add(copyThread);
}
for (Thread thread : threads) {
try {
thread.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println("Copy complete");
}
private List<File> listFile(String from) {
File sourceFile = new File(from);
List<File> files = new ArrayList<>();
if (sourceFile.isFile()) {
files.add(sourceFile);
} else {
try {
listFile(sourceFile, files);
} catch (Exception e) {
e.printStackTrace();
}
}
return files;
}
private void listFile(File file, List<File> lists) {
if (file.isDirectory()) {
for (File f : file.listFiles()) {
listFile(f, lists);
}
} else {
lists.add(file);
}
}
public static void main(String[] args) {
if (args.length < 2) {
usage();
}
String source = args[0];
String destination = args[1];
CopyApp copyApp = new CopyApp();
copyApp.copyFiles(source, destination);
}
static void usage() {
System.err.println("java CopyApp source target-dir");
System.err.println("java CopyApp source-dir target-dir");
System.exit(-1);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment