-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCopyFile.java
More file actions
72 lines (61 loc) · 1.77 KB
/
CopyFile.java
File metadata and controls
72 lines (61 loc) · 1.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
package com.yurii.salimov.lesson06.task06;
import java.io.*;
/**
* @author Yuriy Salimov (yuriy.alex.salimov@gmail.com)
* @version 1.0
*/
final class CopyFile implements Runnable {
private final File inFile;
private final File outFile;
private final IProgress progress;
private int blockSize;
public CopyFile(
final String inFile,
String outFile,
final IProgress progress,
final int blockSize
) {
this.inFile = new File(inFile);
this.outFile = new File(outFile);
this.progress = progress;
this.blockSize = blockSize;
}
@Override
public void run() {
if (this.inFile.exists() && this.inFile.isFile()) {
copy();
} else {
System.out.println("The specified file is not available!");
}
}
private void copy() {
try (FileInputStream fis = new FileInputStream(this.inFile);
FileOutputStream fos = new FileOutputStream(this.outFile);) {
byte[] buffer = new byte[this.blockSize];
int size;
while ((size = fis.read(buffer)) > 0) {
fos.write(buffer, 0, size);
if (this.progress != null) {
this.progress.update((double) size * 100 / this.inFile.length());
}
}
} catch (IOException ex) {
ex.getMessage();
}
}
public File getInputFile() {
return this.inFile;
}
public File getOutputFile() {
return this.outFile;
}
public int getBlockSize() {
return this.blockSize;
}
public void setBlockSize(int size) {
this.blockSize = size;
}
public int getProgress() {
return this.progress.getProgress();
}
}