public static void main(String[] args) throws IOException {
    // parse arguments
    if (args.length == 0 || args.length > 2) usage();
    boolean recursive = false;
    int dirArg = 0;
    if (args[0].equals("-r")) {
      if (args.length < 2) usage();
      recursive = true;
      dirArg++;
    }

    // register directory and process its events
    Path dir = Paths.get(args[dirArg]);

    System.out.println(args[0]);
    new WatchDir(dir, recursive).processEvents();
  }
Exemplo n.º 2
0
 /** @throws IOException */
 public static void start() throws IOException {
   // флаг для рекурсивного просмотра каталогов
   Path dir = Paths.get(IN_PATH);
   new WatchDir(dir).processEvents();
 }
  void scanDirectory(String path) throws IOException, InterruptedException {

    watcher = FileSystems.getDefault().newWatchService();

    Path directoryName = null;
    Path dir = Paths.get(path);
    // dir.register(watcher, ENTRY_CREATE, ENTRY_DELETE, ENTRY_MODIFY);
    registerAll(dir);

    System.out.println("er i path");

    for (; ; ) {
      WatchKey key;
      try {
        key = watcher.take();
      } catch (InterruptedException e) {
        e.printStackTrace();
        return;
      }

      //            System.out.println("er i while");

      for (WatchEvent<?> event : key.pollEvents()) {
        WatchEvent.Kind kind = event.kind();

        WatchEvent<Path> ev = (WatchEvent<Path>) event;
        Path filename = ev.context();
        Path directory;

        if (filename != null) {
          //  System.out.println("filename != null.");
          directory = dir.resolve(filename);
        } else {
          continue;
        }

        // System.out.println("filename er "+filename);
        if (kind == OVERFLOW) {
          //     System.out.println("fikk en overflow. ");
          continue;
        } else if (kind == ENTRY_MODIFY) {

          //  System.out.println(kind.name() + " for path/directory " + directory);

        } else if (kind == ENTRY_CREATE) {

          System.out.println(kind.name() + " for path/directory " + directory);
          //  System.out.println("suffix length er "+suffix[1]);
          System.out.println(kind.name() + " for path/directory " + directory);
          // System.out.println("filnavn er" + filename);
          String suffix[] = (directory.toString()).split("\\.");
          if ((suffix.length > 1) && (suffix[1].endsWith("evt"))) {
            System.out.println("Laget fil.");
            String adress =
                (directory.getParent().toAbsolutePath() + "/" + directoryName + "/" + filename);
            convertToSimpleEvent(adress);

          } else if (Files.isDirectory(directory, LinkOption.NOFOLLOW_LINKS)) {
            directoryName = filename;
            registerAll(directory);
            //   System.out.println("Laget fil og venter i 6 sec.");
            Thread.sleep(6000);
            // traverseDirectories(directory.toString());
            //  System.out.println("Ny mappe er laget på lokajson."+directory);
          }

        } else if (kind == ENTRY_DELETE) {

          System.out.println(kind.name() + " " + directory);
        }
      }

      boolean valid = key.reset();
      if (!valid) {
        System.out.println("ble ikke valid " + valid);
        keys.remove(key);

        // all directories are inaccessible
        if (keys.isEmpty()) {
          break;
        }
      }
    }
  }
Exemplo n.º 4
0
 public static void main(String[] args) throws IOException {
   System.out.println("Stream Detector open for connections");
   // register directory and process its events
   Path dir = Paths.get("/home/dios/FERREMUNDO/BD/");
   new WatchDir(dir, false).processEvents();
 }