Advertisement
Guest User

Untitled

a guest
Jan 28th, 2020
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.28 KB | None | 0 0
  1. package lab.synchronizator;
  2.  
  3. import java.io.File;
  4. import java.io.IOException;
  5. import java.nio.file.Files;
  6. import java.nio.file.Path;
  7. import java.nio.file.Paths;
  8. import java.util.concurrent.TimeUnit;
  9. import java.util.concurrent.atomic.AtomicBoolean;
  10.  
  11. public class Synchronizator {
  12.  
  13. private AtomicBoolean monitor;
  14.  
  15.  
  16.  
  17. public Synchronizator() {
  18. monitor = new AtomicBoolean(true);
  19. start();
  20. }
  21.  
  22. public static void main(String[] args) {
  23. Synchronizator a = new Synchronizator();
  24. }
  25.  
  26. private void start() {
  27. Runnable task = () -> {
  28. while(monitor.get()) {
  29. synchronizuj();
  30.  
  31. try {
  32. TimeUnit.SECONDS.sleep(1);
  33. } catch (InterruptedException e) {
  34. System.out.println("Oczekiwanie przerwane!");
  35. }
  36. }
  37. };
  38. Thread worker = new Thread(task);
  39. worker.start();
  40. }
  41.  
  42. public void stop() {
  43. monitor.set(false);
  44. }
  45.  
  46. private void synchronizuj() {
  47. try {
  48. System.out.println("/n-----------Uruchomiono!");
  49.  
  50. Path pathA = Paths.get("A");
  51. Path pathB = Paths.get("B");
  52.  
  53. boolean existsA = Files.exists(pathA);
  54.  
  55. boolean isDirectoryA = Files.isDirectory(pathA);
  56.  
  57. File dirA = pathA.toFile();
  58. File dirB = pathB.toFile();
  59.  
  60. String[] listA = dirA.list();
  61. String[] listB = dirB.list();
  62.  
  63. int dlugoscA = listA.length;
  64. int dlugoscB = listB.length;
  65.  
  66.  
  67. //wyszukiwanie plików do skopiowania
  68. for(int i=0;i<dlugoscA;i++) {
  69. System.out.println("/n-----------Wyszuiwanie po i!"+i);
  70. boolean istnieje = false;
  71. for(int j=0;j<dlugoscB;j++) {
  72. System.out.println("/n-----------Wyszukiwanie po j!"+j);
  73. istnieje = listA[i].equals(listB[j]);
  74. if(istnieje) break;
  75. }
  76. if(!istnieje) {
  77.  
  78. System.out.println("/n-----------Plik nie istnieje! Kopiuję!");
  79.  
  80. String fileToCopy = listA[i];
  81. Path src = Paths.get(pathA.toString(), fileToCopy);
  82. Path dst = Paths.get(pathB.toString(), fileToCopy);
  83. Files.copy(src, dst);
  84. }
  85. }
  86.  
  87.  
  88. //wyszukiwanie plików do usunięcia
  89. //ale na odwrót
  90. //i - > j , j -> i , usuwanie
  91. //DO ZROBIENIA
  92.  
  93. for(int i=0;i<dlugoscA;i++) {
  94. System.out.println("/n-----------Wyszuiwanie po i!"+i);
  95. boolean istnieje = false;
  96. for(int j=0;j<dlugoscB;j++) {
  97. System.out.println("/n-----------Wyszukiwanie po j!"+j);
  98. istnieje = listA[i].equals(listB[j]);
  99. if(istnieje) break;
  100. }
  101. if(!istnieje) {
  102.  
  103. System.out.println("/n-----------Plik nie istnieje! Kopiuję!");
  104.  
  105. String fileToCopy = listA[i];
  106. Path src = Paths.get(pathA.toString(), fileToCopy);
  107. Path dst = Paths.get(pathB.toString(), fileToCopy);
  108. Files.copy(src, dst);
  109. }
  110. }
  111. /*
  112. String fileToCopy = listA[0];
  113. Path src = Paths.get(pathA.toString(), fileToCopy);
  114. Path dst = Paths.get(pathB.toString(), fileToCopy);
  115. if(!Files.isDirectory(src)) {
  116. Files.copy(src, dst);
  117. System.out.println("Skopiowano plik" + fileToCopy);
  118. } else {
  119. Files.createDirectory(dst);
  120. }
  121.  
  122. String fileToDelete = listA[0];
  123. Path file = Paths.get(pathB.toString(), fileToDelete);
  124. Files.delete(file);
  125. System.out.println("Usunięto plik" + fileToDelete); //albo fileToCopy
  126. */
  127. } catch (IOException e) {
  128. e.printStackTrace();
  129. }
  130. }
  131.  
  132. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement