简体   繁体   English

想要在Java的属性文件中定义进程的状态序列

[英]Want to define state-sequence of processes in the property file in Java

I am planning to build a program which keeps on looking the directory, and if any new file comes then it'll notify me. 我计划构建一个继续查找目录的程序,如果有新文件出现,它将通知我。 But I don't want to use Watch Service API. 但是我不想使用Watch Service API。 Is there any other simple logic to do the same? 还有其他简单的逻辑可以做到吗? without using any third party API or library? 不使用任何第三方API或库?

The Watch Service API is the simplest and most efficient way to do this, which is why it exists. Watch Service API是执行此操作的最简单,最有效的方法,这就是它存在的原因。

You can however, poll the directory and look for changes. 但是,您可以轮询目录并查找更改。 This has some down sides. 这有一些缺点。

  • It is hard to detect all possible changes. 很难检测到所有可能的更改。
  • It can be really inefficient and create a lot of garbage if you do this regularly or have a lot of files or both. 如果您定期执行此操作或拥有大量文件或同时执行这两个操作,则它的效率可能非常低,并且会造成大量垃圾。
  • It can be error prone if you haven't implemented this before. 如果您以前没有实现过此功能,则可能容易出错。

Is there any other simple logic to do the same? 还有其他简单的逻辑可以做到吗?

Repeatedly call 反复打电话

File dir = new File(directoryName);
// note: creates a lot of garbage even if nothing changed.
File[] files = dir.listFiles(); 
if (files != null) {
    // check for changes.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM