-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy path7.2 java
More file actions
50 lines (44 loc) · 1.79 KB
/
7.2 java
File metadata and controls
50 lines (44 loc) · 1.79 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
class MyFileVisitor extends SimpleFileVisitor<Path> {
private ArrayList<Path> paths = new ArrayList<>();
public ArrayList<Path> getPaths() {
return paths;
}
@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
paths.add(file);
return FileVisitResult.CONTINUE;
}
}
public class Utils {
public static long calculateFolderSize(String path) {
File mypath = new File(path);
long length = 0;
if (!mypath.isDirectory() || !mypath.exists()) throw new IllegalArgumentException();
for (File file : mypath.listFiles()) {
length += file.length();
}
return length;
}
public static void copyFolder(String sourceDirectory, String destinationDirectory) {
File directory = new File(sourceDirectory);
Path mainPath = Paths.get(sourceDirectory);
Path copyDir = Paths.get(destinationDirectory);
if (!directory.exists()) throw new IllegalArgumentException();
else
{
try{
if(!Files.exists(copyDir)) Files.createDirectory(copyDir);
MyFileVisitor myFileVisitor = new MyFileVisitor();
Files.walkFileTree(mainPath,myFileVisitor);
for(Path path : myFileVisitor.getPaths()){
Path test = new File(destinationDirectory+File.separator+path).toPath();
Files.copy(path,new File(copyDir+File.separator+path.getFileName()).toPath(),StandardCopyOption.REPLACE_EXISTING);
}
}catch (IOException e)
{
e.printStackTrace();
}
}
//TODO реализовать метод копирования папки sourceDirectory в destinationDirectory
}
}