简体   繁体   中英

How can I get FileWatcher to tell me the file modified is a folder?

In Java, I am using the procedure presented here . Everything is working fine when I am interacting with files, but I am running into problems when dealing with folders. The code does indeed detect that a new folder has been added/modified etc. but it does not tell me that is a folder and not a file.

It will say "foo has been added", this means foo can be a file or folder, and when I am trying to build a link out of that information, it matters.

What is the solution that I should adopt in this case?

You can check whether it is a file or folder by using isFile() or isDirectory()

String filePath = watchEvent.context().toString();
File file = new File(filePath);
//here you can identify whether it is file or folder isFile() or isDirectory()
if(file.isFile()){
  //is a file 
}
if(file.isDirectory()){
 //is a directory
}

Hope the above may help you .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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