Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package lab.synchronizator;
- import java.io.File;
- import java.io.IOException;
- import java.nio.file.Files;
- import java.nio.file.Path;
- import java.nio.file.Paths;
- import java.util.concurrent.TimeUnit;
- import java.util.concurrent.atomic.AtomicBoolean;
- public class Synchronizator {
- private AtomicBoolean monitor;
- public Synchronizator() {
- monitor = new AtomicBoolean(true);
- start();
- }
- public static void main(String[] args) {
- Synchronizator a = new Synchronizator();
- }
- private void start() {
- Runnable task = () -> {
- while(monitor.get()) {
- synchronizuj();
- try {
- TimeUnit.SECONDS.sleep(1);
- } catch (InterruptedException e) {
- System.out.println("Oczekiwanie przerwane!");
- }
- }
- };
- Thread worker = new Thread(task);
- worker.start();
- }
- public void stop() {
- monitor.set(false);
- }
- private void synchronizuj() {
- try {
- System.out.println("/n-----------Uruchomiono!");
- Path pathA = Paths.get("A");
- Path pathB = Paths.get("B");
- boolean existsA = Files.exists(pathA);
- boolean isDirectoryA = Files.isDirectory(pathA);
- File dirA = pathA.toFile();
- File dirB = pathB.toFile();
- String[] listA = dirA.list();
- String[] listB = dirB.list();
- int dlugoscA = listA.length;
- int dlugoscB = listB.length;
- //wyszukiwanie plików do skopiowania
- for(int i=0;i<dlugoscA;i++) {
- System.out.println("/n-----------Wyszuiwanie po i!"+i);
- boolean istnieje = false;
- for(int j=0;j<dlugoscB;j++) {
- System.out.println("/n-----------Wyszukiwanie po j!"+j);
- istnieje = listA[i].equals(listB[j]);
- if(istnieje) break;
- }
- if(!istnieje) {
- System.out.println("/n-----------Plik nie istnieje! Kopiuję!");
- String fileToCopy = listA[i];
- Path src = Paths.get(pathA.toString(), fileToCopy);
- Path dst = Paths.get(pathB.toString(), fileToCopy);
- Files.copy(src, dst);
- }
- }
- //wyszukiwanie plików do usunięcia
- //ale na odwrót
- //i - > j , j -> i , usuwanie
- //DO ZROBIENIA
- for(int i=0;i<dlugoscA;i++) {
- System.out.println("/n-----------Wyszuiwanie po i!"+i);
- boolean istnieje = false;
- for(int j=0;j<dlugoscB;j++) {
- System.out.println("/n-----------Wyszukiwanie po j!"+j);
- istnieje = listA[i].equals(listB[j]);
- if(istnieje) break;
- }
- if(!istnieje) {
- System.out.println("/n-----------Plik nie istnieje! Kopiuję!");
- String fileToCopy = listA[i];
- Path src = Paths.get(pathA.toString(), fileToCopy);
- Path dst = Paths.get(pathB.toString(), fileToCopy);
- Files.copy(src, dst);
- }
- }
- /*
- String fileToCopy = listA[0];
- Path src = Paths.get(pathA.toString(), fileToCopy);
- Path dst = Paths.get(pathB.toString(), fileToCopy);
- if(!Files.isDirectory(src)) {
- Files.copy(src, dst);
- System.out.println("Skopiowano plik" + fileToCopy);
- } else {
- Files.createDirectory(dst);
- }
- String fileToDelete = listA[0];
- Path file = Paths.get(pathB.toString(), fileToDelete);
- Files.delete(file);
- System.out.println("Usunięto plik" + fileToDelete); //albo fileToCopy
- */
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement