fork download
  1. package com.mycompany;
  2.  
  3. import java.io.IOException;
  4. import java.nio.file.*;
  5. import java.util.Scanner;
  6.  
  7. import static java.nio.file.StandardWatchEventKinds.*;
  8.  
  9. public class App {
  10. public static void main(String[] args) throws IOException, InterruptedException {
  11. WatchService watcher = FileSystems.getDefault().newWatchService();
  12. Path path = FileSystems.getDefault().getPath("/home", "vpupkin");
  13. System.out.println(path.toString());
  14. WatchKey key =
  15. path.register(watcher, ENTRY_CREATE, ENTRY_DELETE, ENTRY_MODIFY);
  16. Thread t = new Thread(() -> {
  17. do {
  18. WatchKey watchKey = null;
  19. try {
  20. watchKey = watcher.take();
  21. } catch (InterruptedException e) {
  22. return;
  23. }
  24. for (WatchEvent<?> event : watchKey.pollEvents()) {
  25. WatchEvent.Kind<?> kind = event.kind();
  26. if (kind == OVERFLOW) {
  27. continue;
  28. }
  29. WatchEvent<Path> ev = (WatchEvent<Path>) event;
  30. Path filename = ev.context();
  31. Path child = path.resolve(filename);
  32. String contentType = null;
  33. try {
  34. contentType = Files.probeContentType(child);
  35. } catch (IOException e) {
  36. e.printStackTrace();
  37. continue;
  38. }
  39. if (null == contentType) {
  40. System.out.println("Null");
  41. continue;
  42. }
  43. if (!contentType.equals("text/plain")) {
  44. System.err.format("New file '%s'" +
  45. " is not a plain text file.%n", filename);
  46. continue;
  47. }
  48. System.out.format("Emailing file %s%n", filename);
  49. }
  50. boolean valid = key.reset();
  51. if (!valid) {
  52. return;
  53. }
  54. } while (true);
  55. });
  56. t.start();
  57. Scanner sc = new Scanner(System.in);
  58. String next;
  59. do {
  60. next = sc.next();
  61. } while (!next.equals("stop"));
  62. t.interrupt();
  63. System.out.println("Bye!");
  64. }
  65. }
  66.  
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
Main.java:9: error: class App is public, should be declared in a file named App.java
public class App {
       ^
Note: Main.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
1 error
stdout
Standard output is empty