在Java中创建一个监听器。

huangapple go评论61阅读模式
英文:

Create a Listener in Java

问题

我想在Java/Spring中创建一个程序,始终监听一个目录。如果在此目录中创建了一个名为"example.txt"的特定文件,则程序会读取它并执行一些操作,然后继续监听。如果没有创建此txt文件,则什么都不会发生,它会继续监听。

您对如何创建此监听部分有什么想法吗?

非常感谢您的帮助。

英文:

I want to create a program in Java/Spring that listens always a directory. If a particular called for example "example.txt" is created in this directory then the program reads it and do some stuff and then continues listening. If this txt file is not created then nothing happens and it continues listening.

Any idea about how can I create this listener part?

Thank you so much for your help

答案1

得分: 1

你可以使用在Java 7中新增的WatchService类。

WatchService watchService = FileSystems.getDefault().newWatchService();
Path path = Paths.get("example.txt");
WatchKey watchKey = path.register(watchService, StandardWatchEventKinds...);

更多信息请参见这里这里

英文:

You can use the WatchService added in Java 7

WatchService watchService = FileSystems.getDefault().newWatchService();
Path path = Paths.get("example.txt");
WatchKey watchKey = path.register(watchService, StandardWatchEventKinds...);

More information here and here

huangapple
  • 本文由 发表于 2020年10月1日 17:57:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/64153025.html
  • concurrency
  • java
  • listener
  • spring

如何在使用map时避免覆盖for循环中的List(Object[])部分。 go 67
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定

  • 开发者交流平台

    本页二维码