import java io import java util ArrayList import java util Collections

 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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
import java.io.*;
import java.util.ArrayList;
import java.util.Collections;
public class Sync {
private static int compare(File file1, File file2) throws IOException {
BufferedInputStream in = new BufferedInputStream(new FileInputStream(file1));
BufferedInputStream in2 = new BufferedInputStream(new FileInputStream(file2));
int size1, size2;
if(file1.length() != file2.length()) return 1;
else{
while ((size1 = in.read()) != -1 && (size2 = in2.read()) != -1) {
if(size1 != size2) return 1;
}
}
return 0;
}
public static void function(String[] D_list, String[] S_list, int size1, int size2, ArrayList<String> result_C, ArrayList<String> result_D, File D, File S, String help) throws IOException {
int i, k;
for(i = 0; i < size1; i++){
File file = new File(D+"/"+D_list[i]);
if(file.isFile()){
File file2 = new File(S+"/"+D_list[i]);
if(!file2.exists()){
result_D.add(help+D_list[i]);
}
}
}
for(i = 0; i < size2; i++){
File file_or_directory = new File(S.getPath()+"/"+S_list[i]);
if(file_or_directory.isFile()){
File temp4 = new File(D.getPath()+"/"+S_list[i]);
if(temp4.exists()) {
if(compare(temp4, file_or_directory) == 1){
result_D.add(help+S_list[i]);
result_C.add(help+S_list[i]);
}
}
else result_C.add(help+S_list[i]);
}
else if(file_or_directory.isDirectory()){
File temp4 = new File(D.getPath()+"/"+S_list[i]);
if(temp4.exists()){
String[] temp = temp4.list(), temp1 = new File(S.getPath() + "/"+S_list[i]).list();
File temp2 = new File(D.getPath()+"/"+S_list[i]), temp3 = new File(S.getPath()+"/"+S_list[i]);
String help2 = help+S_list[i]+"/";
function(temp, temp1, temp.length, temp1.length, result_C, result_D, temp2, temp3, help2);
}
else{
File q = file_or_directory;
String[] temp = q.list();
int length = temp.length;
for(k = 0; k < length; k++){
result_C.add(help+q.getName()+"/"+temp[k]);
}
}
}
}
}
public static void main(String[] args) throws IOException {
int i;
File S = new File(args[0]), D = new File(args[1]);
String[] D_list = D.list(), S_list = S.list();
int size1 = D_list.length, size2 = S_list.length;
ArrayList<String> result_C = new ArrayList<String>(), result_D = new ArrayList<String>();
String help = "";
if(size1 == 0 && size2 == 0){
System.out.println("IDENTICAL");
return;
}
function(D_list, S_list, size1, size2, result_C, result_D, D, S, help);
if(result_C.size() == 0 && result_D.size() == 0){
System.out.println("IDENTICAL");
return;
}
Collections.sort(result_C);
Collections.sort(result_D);
for(i = 0; i < result_D.size(); i++){
System.out.print("DELETE" + " ");
System.out.print(result_D.get(i));
System.out.println();
}
for(i = 0; i < result_C.size(); i++){
System.out.print("COPY" + " ");
System.out.print(result_C.get(i));
System.out.println();
}
}
}