Advertisement
Guest User

Untitled

a guest
Jan 27th, 2020
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.74 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. public Synchronizator() {
  16. monitor = new AtomicBoolean(true);
  17. start();
  18. }
  19.  
  20. private void start() {
  21. Runnable task = () -> {
  22. while(monitor.get()) {
  23. synchronizuj();
  24.  
  25. try {
  26. TimeUnit.SECONDS.sleep(1);
  27. } catch (InterruptedException e) {
  28. System.out.println("Oczekiwanie przerwane!");
  29. }
  30. }
  31. };
  32. Thread worker = new Thread(task);
  33. worker.start();
  34. }
  35.  
  36. public void stop() {
  37. monitor.set(false);
  38. }
  39.  
  40. private void synchronizuj() {
  41. try {
  42. Path pathA = Paths.get("A/");
  43. Path pathB = Paths.get("B/");
  44.  
  45. boolean existsA = Files.exists(pathA);
  46.  
  47. boolean isDirectoryA = Files.isDirectory(pathA);
  48.  
  49. File dirA = pathA.toFile();
  50. File dirB = pathB.toFile();
  51.  
  52. String[] listA = dirA.list();
  53. String[] listB = dirB.list();
  54.  
  55. //wyszukiwanie plików do skopiowania lub usunięcia
  56.  
  57. String fileToCopy = listA[0];
  58. Path src = Paths.get(pathA.toString(), fileToCopy);
  59. Path dst = Paths.get(pathB.toString(), fileToCopy);
  60. if(!Files.isDirectory(src)) {
  61. Files.copy(src, dst);
  62. System.out.println("Skopiowano plik" + fileToCopy);
  63. } else {
  64. Files.createDirectory(dst);
  65. }
  66.  
  67. String fileToDelete = listA[0];
  68. Path file = Paths.get(pathB.toString(), fileToDelete);
  69. Files.delete(file);
  70. System.out.println("Usunięto plik" + fileToDelete);
  71.  
  72. } catch (IOException e) {
  73. e.printStackTrace();
  74. }
  75. }
  76.  
  77. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement