Advertisement
Guest User

Untitled

a guest
Apr 18th, 2015
230
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.06 KB | None | 0 0
  1. package com.company;
  2. import java.io.*;
  3. import java.nio.file.*;
  4. import java.nio.file.attribute.BasicFileAttributes;
  5. import java.util.*;
  6.  
  7.  
  8. public class Sync {
  9.     static Path pathS;
  10.     static Path pathD;
  11.     static Set <Path> copyHashSet   = new HashSet<Path>();
  12.     static Set <Path> deleteHashSet = new HashSet<Path>();
  13.  
  14.     static boolean FileComparator (File fileA, File fileB) throws IOException {
  15.         if (fileA.length() != fileB.length()) return false;
  16.         else {
  17.             BufferedInputStream inA = new BufferedInputStream(new FileInputStream(fileA));
  18.             BufferedInputStream inB = new BufferedInputStream(new FileInputStream(fileB));
  19.             char cA, cB;
  20.             while (inA.available() > 0  && inB.available() > 0) {
  21.                 cA = (char) inA.read();
  22.                 cB = (char) inB.read();
  23.                 if (cA != cB) return false;
  24.             }
  25.         }
  26.         return true;
  27.     }
  28.  
  29.  
  30.     static class CopyingFileVisitor extends SimpleFileVisitor<Path>  {
  31.  
  32.         @Override
  33.         public FileVisitResult visitFile(Path file, BasicFileAttributes attr) throws IOException {
  34.             copyHashSet.add(file.subpath(1, file.getNameCount())); // adding path
  35.             //System.out.println(file.getNameCount());     // getNameCount returns number of folders in current path (starting from S )
  36.             return FileVisitResult.CONTINUE;
  37.         }
  38.     }
  39.  
  40.     static class DeletingFileVisitor extends SimpleFileVisitor <Path> {
  41.  
  42.  
  43.         @Override
  44.         public FileVisitResult visitFile(Path file, BasicFileAttributes attr) throws IOException {
  45.             Path d = file.subpath(1, file.getNameCount());
  46.             Path s = FileSystems.getDefault().getPath(pathS.toString() + "/" + d.toString());
  47.             if (copyHashSet.contains(d) && FileComparator(s.toFile(), file.toFile())) copyHashSet.remove(d); // both files exist and equal
  48.             else deleteHashSet.add(d); // both files exist but not equal or  file does exist in D but not in S
  49.             return FileVisitResult.CONTINUE;
  50.         }
  51.     }
  52.  
  53.     public static void main(String[] args) throws IOException {
  54.         // write your code here
  55.         File S = new File(args[0]);
  56.         File D = new File(args[1]);
  57.         pathS = S.toPath();
  58.         pathD = D.toPath();
  59.         Files.walkFileTree(pathS, new CopyingFileVisitor());
  60.         Files.walkFileTree(pathD, new DeletingFileVisitor());
  61.         if (copyHashSet.isEmpty() && deleteHashSet.isEmpty()) {
  62.             System.out.println("IDENTICAL");
  63.         } else {
  64.             ArrayList <Path> copyList = new ArrayList<Path>(copyHashSet);
  65.             ArrayList <Path> deleteList = new ArrayList<Path>(deleteHashSet);
  66.             Collections.sort(copyList);
  67.             Collections.sort(deleteList);
  68.  
  69.             for (int i = 0; i < deleteList.size(); i++) {
  70.                 System.out.println("DELETE " + deleteList.get(i));
  71.             }
  72.  
  73.             for (int i = 0; i < copyList.size(); i++) {
  74.                 System.out.println("COPY " + copyList.get(i));
  75.             }
  76.         }
  77.     }
  78. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement