Skip to content

Instantly share code, notes, and snippets.

@kryvoboker
Last active July 29, 2020 17:59
Show Gist options
  • Save kryvoboker/cb210d6c9e3cbb84f5699b40c1e59bcf to your computer and use it in GitHub Desktop.
Save kryvoboker/cb210d6c9e3cbb84f5699b40c1e59bcf to your computer and use it in GitHub Desktop.
HomeWork5 (многопоточное программирование часть 1)
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Arrays;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
*
* @author kamaz
*/
public class CopyFile implements Runnable {
private File file;
private File[] folder;
public CopyFile(File file, File[] folder) {
this.file = file;
this.folder = folder;
}
public CopyFile() {
super();
}
public File getFile() {
return file;
}
public File[] getFolder() {
return folder;
}
private static void copyFiles(File in, File out) throws IOException {
byte[] buffer = new byte[1024 * 1024];
int readByte = 0;
if (in.isFile()) {
try (FileInputStream fis = new FileInputStream(in);
FileOutputStream fos = new FileOutputStream(out)) {
for (; (readByte = fis.read(buffer)) > 0;) {
fos.write(buffer, 0, readByte);
}
} catch (IOException e) {
System.out.println("ERROR three");
}
}
}
@Override
public void run() {
File folderCreate = new File("files");
folderCreate.mkdirs();
file = new File(".");
folder = file.listFiles();
for (File fileIn : folder) {
Thread th = Thread.currentThread();
File outCreate = new File(folderCreate.getName() + "/" + fileIn.getName());
try {
if (fileIn.isFile()) {
outCreate.createNewFile();
}
} catch (IOException ex) {
System.out.println("ERROR one");
}
try {
copyFiles(fileIn, outCreate);
} catch (IOException ex) {
System.out.println("ERROR two");
}
}
}
}
public class Main {
public static void main(String[] args) {
Thread th = new Thread(new CopyFile());
th.start();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment