简体   繁体   English

如何让FileWatcher告诉我修改后的文件是文件夹?

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

In Java, I am using the procedure presented here . 在Java中,我正在使用此处介绍的过程。 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. 它会说“已经添加了foo”,这意味着foo可以是文件或文件夹,而当我尝试根据该信息构建链接时,这很重要。

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() 您可以使用isFile()或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 . 希望以上内容对您有所帮助。

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

相关问题 我可以设置Git在每次修改某些文件时告诉我 - Can I setup Git to tell me each time a certain file(s) has been modified 我怎样才能让java告诉我文件中正确的行数和单词数? - How can I get java to tell me the correct amount of lines and words in a file? java.nio.file.WatchEvent 只给我相对路径。 如何获取修改后文件的绝对路径? - java.nio.file.WatchEvent gives me only relative path. How can I get the absolute path of the modified file? 如果上次修改日期超过特定时间,我如何告诉 Camel 只复制文件? - How can I tell Camel to only copy a file if the Last Modified date is past a certain time? 如何获取列表(搜索)以告诉我每个列表是否都在另一个列表(主列表)中? - How can I get the List (Search) to tell me whether or not each it is in the other List (main)? 有人可以告诉我为什么我得到这个输出吗? - Can someone tell me why I get this output? 在Java中,我可以要求系统告诉我文件的字符集吗? - In Java can I ask the system to tell me the charset of a file? 有人可以告诉我如何在Eclipse中显示菜单栏吗? - Can someone tell me how to get the menu bar to show in Eclipse? JCIFS-如何获取文件夹中最后修改的文件的名称? - JCIFS - How to get name of last modified file in folder? 日食怎么能告诉我我目前正在进行的活动? - how can eclipse tell me in which activity i'm currently in?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM