Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.*;
- import java.util.*;
- public class Sync {
- static boolean flag = true;
- static final String[] cmd = new String[]{"DELETE ", "COPY "};
- public static void main(String[] args) {
- File first = new File(args[0]), second = new File(args[1]);
- delete(first, second, args);
- copy(second, first, args);
- if (flag) {
- System.out.println("IDENTICAL");
- }
- }
- public static void delete(File first, File second, String[] args){
- if (first.exists()) {
- ArrayList<String> list = new ArrayList<>(Arrays.asList(first.list()));
- File[] arr = second.listFiles();
- list.sort(Comparator.naturalOrder());
- Arrays.sort(arr, Comparator.comparing(x -> x.getName()));
- for (File f : arr) {
- if (f.isDirectory()) {
- delete(new File(first, f.getName()), f, args);
- } else if (!list.contains(f.getName())) {
- flag = false;
- System.out.println("DELETE " + f.toString().substring(args[0].length() + 1));
- } else {
- try {
- File file = new File(first, f.getName());
- byte[] bf = new byte[(int) f.length()], bd = new byte[(int) file.length()];
- FileInputStream stream = new FileInputStream(f);
- stream.read(bf);
- stream.close();
- stream = new FileInputStream(file);
- stream.read(bd);
- stream.close();
- if (!Arrays.equals(bf, bd)) {
- flag = false;
- System.out.println("DELETE " + f.toString().substring(args[0].length() + 1));
- }
- } catch (IOException e) {
- }
- }
- }
- } else {
- flag = false;
- File[] arr = second.listFiles();
- Arrays.sort(arr, Comparator.comparing(x -> x.getName()));
- for (File f : arr) {
- System.out.println("DELETE " + f.toString().substring(args[0].length() + 1));
- }
- }
- }
- public static void copy(File first, File second, String[] args){
- if (first.exists()) {
- ArrayList<String> list = new ArrayList<>(Arrays.asList(first.list()));
- File[] arr = second.listFiles();
- list.sort(Comparator.naturalOrder());
- Arrays.sort(arr, Comparator.comparing(x -> x.getName()));
- for (File f : arr) {
- if (f.isDirectory()) {
- copy(new File(first, f.getName()), f, args);
- } else if (!list.contains(f.getName())) {
- flag = false;
- System.out.println("COPY " + f.toString().substring(args[1].length() + 1));
- } else {
- try {
- File file = new File(first, f.getName());
- byte[] bf = new byte[(int) f.length()], bd = new byte[(int) file.length()];
- FileInputStream stream = new FileInputStream(f);
- stream.read(bf);
- stream.close();
- stream = new FileInputStream(file);
- stream.read(bd);
- stream.close();
- if (!Arrays.equals(bf, bd)) {
- flag = false;
- System.out.println("COPY " + f.toString().substring(args[1].length() + 1));
- }
- } catch (IOException e) {
- }
- }
- }
- } else {
- flag = false;
- File[] arr = second.listFiles();
- Arrays.sort(arr, Comparator.comparing(x -> x.getName()));
- for (File f : arr) {
- System.out.println("COPY " + f.toString().substring(args[0].length() + 1));
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement