-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathCopywriter.java
54 lines (49 loc) · 1.89 KB
/
Copywriter.java
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
package com.yurii.salimov.lesson05.task04_05;
import java.io.*;
import java.sql.Date;
import java.text.SimpleDateFormat;
/**
* @author Yurii Salimov ([email protected])
*/
public final class Copywriter {
public void copy(final String fromDirPath, final String toDirPath) throws IOException {
final File fromDir = new File(fromDirPath);
final File toDir = new File(toDirPath);
if (!fromDir.exists()) {
System.out.println("The specified directory is not available!");
} else {
if (!toDir.exists()) {
toDir.mkdirs();
}
for (File file : fromDir.listFiles()) {
if (file.isFile()) {
copyFile(file.getPath(), toDirPath + "\\" + file.getName());
}
}
}
}
public void copyFile(final String from, final String to) throws IOException {
try (BufferedReader br = new BufferedReader(new FileReader(from));
BufferedWriter bw = new BufferedWriter(new FileWriter(to))) {
int buf;
while ((buf = br.read()) != -1) {
bw.write(buf);
}
}
}
public void aboutFiles(final String dirPath) throws IOException {
final File dir = new File(dirPath);
if (!dir.exists()) {
System.out.println("The specified directory is not available!");
} else {
final SimpleDateFormat dateFormat = new SimpleDateFormat("dd.MM.yyyy hh:mm:ss");
try (final BufferedWriter bw = new BufferedWriter(new FileWriter(dirPath + "\\" + "info.txt"))) {
for (File file : dir.listFiles()) {
if (file.isFile()) {
bw.write(file.getName() + ", date: " + dateFormat.format(new Date(file.lastModified())) + "\r\n");
}
}
}
}
}
}