From a22078bc17a85ac05024fce5cec1fd3bba1a4b1d Mon Sep 17 00:00:00 2001 From: Stefan Begerad Date: Tue, 23 Nov 2021 12:20:40 -0500 Subject: [PATCH] feat(mon_dir): initial commit --- mon_dir/.gitignore | 31 +++++++ mon_dir/README.md | 5 ++ mon_dir/pom.xml | 86 +++++++++++++++++++ .../main/java/de/swingbe/mon_dir/Main.java | 44 ++++++++++ 4 files changed, 166 insertions(+) create mode 100644 mon_dir/.gitignore create mode 100644 mon_dir/README.md create mode 100644 mon_dir/pom.xml create mode 100644 mon_dir/src/main/java/de/swingbe/mon_dir/Main.java diff --git a/mon_dir/.gitignore b/mon_dir/.gitignore new file mode 100644 index 0000000..33653da --- /dev/null +++ b/mon_dir/.gitignore @@ -0,0 +1,31 @@ + +# Files +.classpath +.externalToolBuilders +.gradle +.project +.pydevproject +.settings +.sonar +*~ +*.ipr +*.iml +*.iws +*.swp +*.DS_Store +*.snap.debug +dependency-reduced-pom.xml + +# Directories +.idea/ +.run/ +.venv/ +_site/ +build/ +dist/ +docs/_build/ +gen-java/ +gen-javabean/ +gen-py/ +node_modules/ +target/ diff --git a/mon_dir/README.md b/mon_dir/README.md new file mode 100644 index 0000000..184299a --- /dev/null +++ b/mon_dir/README.md @@ -0,0 +1,5 @@ +# Overview +This project can serve as starting point for Maven and Java based projects. + +# Links +[source](https://javapapers.com/core-java/monitor-a-folder-using-java/) \ No newline at end of file diff --git a/mon_dir/pom.xml b/mon_dir/pom.xml new file mode 100644 index 0000000..569c3dd --- /dev/null +++ b/mon_dir/pom.xml @@ -0,0 +1,86 @@ + + + 4.0.0 + + mon_dir + description + https://swingbe.de + org.swingbe.de + mon_dir + 0.0.1 + jar + + + + GNU General Public License + https://www.gnu.org/licenses/gpl-3.0.txt + + + + + https://github.com/Software-Ingenieur-Begerad/sandbox-java + + + + + UTF-8 + + + + + + org.apache.maven.plugins + maven-compiler-plugin + 3.8.1 + + + 11 + + + + org.apache.maven.plugins + maven-shade-plugin + 3.2.1 + + + package + + shade + + + + + + *:* + + META-INF/*.SF + META-INF/*.DSA + META-INF/*.RSA + + + + + true + + shaded + + + + + de.swingbe.mon_dir.Main + + + + + + + + + + + diff --git a/mon_dir/src/main/java/de/swingbe/mon_dir/Main.java b/mon_dir/src/main/java/de/swingbe/mon_dir/Main.java new file mode 100644 index 0000000..100b62d --- /dev/null +++ b/mon_dir/src/main/java/de/swingbe/mon_dir/Main.java @@ -0,0 +1,44 @@ +package de.swingbe.mon_dir; + +import java.io.IOException; +import java.nio.file.*; + +public class Main { + + public static void main(String[] args) throws IOException, + InterruptedException { + + Path folder = Paths.get("./"); + + //obtain WatchService instance using FileSystems class + WatchService watchService = FileSystems.getDefault().newWatchService(); + + //register path to be watched with this instance + folder.register(watchService, StandardWatchEventKinds.ENTRY_CREATE); + + boolean valid = true; + do { + WatchKey watchKey = watchService.take(); + + for (WatchEvent event : watchKey.pollEvents()) { + WatchEvent.Kind kind = event.kind(); + + if (StandardWatchEventKinds.ENTRY_CREATE.equals(event.kind())) { + String fileName = event.context().toString(); + System.out.println("File Created:" + fileName); + } else if (StandardWatchEventKinds.ENTRY_MODIFY.equals(event.kind())) { + String fileName = event.context().toString(); + System.out.println("File modified:" + fileName); + } else if (StandardWatchEventKinds.ENTRY_DELETE.equals(event.kind())) { + String fileName = event.context().toString(); + System.out.println("File deleted:" + fileName); + } else { + System.out.println("Event unkown"); + } + valid = watchKey.reset(); + + } + } + while (valid); + } +} \ No newline at end of file