Java:看目录来移动大文件 [英] Java: Watching a directory to move large files
问题描述
我一直在编写一个程序来监视一个目录,当文件被创建时,它会改变名字并将它们移动到一个新的目录。在我的第一个实现中,我使用Java的Watch Service API,当我测试1kb文件时,它的工作正常。出现的问题是,实际上,创建的文件可以在50-300mb之间。当这种情况发生时,观察者API会立即找到该文件,但由于仍然被写入而无法移动它。我试着把观察者放在一个循环中(生成异常,直到文件可以移动),但是这似乎效率很低。
既然没有工作,我试过了使用定时器,每10秒检查一次文件夹,然后移动文件即可。这是我结束的方法。
问题:无论如何,当文件完成时,无需进行异常检查或持续比较大小?我喜欢为每个文件使用Watcher API一次的想法,而不是使用定时器持续检查(并运行到异常)。
所有的响应都非常感谢! / p>
nt
写另一个文件表示原始文件完成了。
I.g'fileorg.dat'如果完成创建一个文件'fileorg.done',并且只为'fileorg.done'检查
,就会增长。
使用巧妙的命名约定,您不应该有问题。
I have been writing a program that watches a directory and when files are created in it, it changes the name and moves them to a new directory. In my first implementation I used Java's Watch Service API which worked fine when I was testing 1kb files. The problem that came up is that in reality the files getting created are anywhere from 50-300mb. When this happened the watcher API would find the file right away but could not move it because it was still being written. I tried putting the watcher in a loop (which generated exceptions until the file could be moved) but this seemed pretty inefficient.
Since that didn't work, I tried up using a timer that checks the folder every 10s and then moves files when it can. This is the method I ended up going for.
Question: Is there anyway to signal when a file is done being written without doing an exception check or continually comparing the size? I like the idea of using the Watcher API just once for each file instead of continually checking with a timer (and running into exceptions).
All responses are greatly appreciated!
nt
Write another file as an indication that the original file is completed. I.g 'fileorg.dat' is growing if done create a file 'fileorg.done' and check only for the 'fileorg.done'.
With clever naming conventions you should not have problems.
这篇关于Java:看目录来移动大文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!