I have a directory in aix which will receive files from a external application. My java program should pick only those files which are completely written by the application.It should not pick the incomplete files which are being still written by the external application. Can someone help with a sample?
我在aix中有一个目录,它将从外部应用程序接收文件。我的java程序应该只选择那些完全由应用程序编写的文件。它不应该选择仍然由外部应用程序写入的不完整文件。有人可以帮忙提供样品吗?
4 个解决方案
#1
1
Try Apache Camel File Poller and stratergies OS or file renaiming to detect file access in progress
尝试使用Apache Camel File Poller和stratergies OS或文件renaiming来检测正在进行的文件访问
#2
1
One common way to solve this problem is to rename the file once it was completely written.
解决此问题的一种常见方法是在文件完全写入后重命名该文件。
For example your application writes myFile.txt.tmp and once it is done it renames it to myFile.txt. Doing this you simply ignore the files ending in .tmp...
例如,您的应用程序会写入myFile.txt.tmp,一旦完成,它会将其重命名为myFile.txt。这样做你只需忽略以.tmp结尾的文件...
Or, as already said, by alexcpn have a look at the Apache Camel File Component...
或者,正如已经说过的,由alexcpn看看Apache Camel文件组件......
#3
0
My approach would be to have the files written to a "temporary" directory first. After writing a file has finished, it should be moved to the final destination. Take care to have the "temporary" and the "final" directory on the same filesystem; otherwise, File.renameTo()
might or might not work. When using Java 7, Files.move()
might be of better use.
我的方法是首先将文件写入“临时”目录。写完文件后,应该将其移动到最终目的地。注意在同一个文件系统上有“临时”和“最终”目录;否则,File.renameTo()可能会也可能不会。使用Java 7时,可能更好地使用Files.move()。
Obviously, your application only has to check for files in the "final" directory, since you know all files in that directory are completely written.
显然,您的应用程序只需检查“final”目录中的文件,因为您知道该目录中的所有文件都已完全写入。
#4
0
In Windows there is no way to know when a file is locked for writing by another application, I've hit the same problem before. You can try using JNotify to listen for events and treat the exceptions that occur, as far as i know there is no other way, but in Unix checking if a file is locked should do the job new File(fileName).canWrite()
在Windows中,没有办法知道文件何时被另一个应用程序锁定以供写入,我之前遇到过同样的问题。您可以尝试使用JNotify监听事件并处理发生的异常,据我所知没有其他办法,但在Unix中检查文件是否被锁定应该执行作业新文件(fileName).canWrite()
#1
1
Try Apache Camel File Poller and stratergies OS or file renaiming to detect file access in progress
尝试使用Apache Camel File Poller和stratergies OS或文件renaiming来检测正在进行的文件访问
#2
1
One common way to solve this problem is to rename the file once it was completely written.
解决此问题的一种常见方法是在文件完全写入后重命名该文件。
For example your application writes myFile.txt.tmp and once it is done it renames it to myFile.txt. Doing this you simply ignore the files ending in .tmp...
例如,您的应用程序会写入myFile.txt.tmp,一旦完成,它会将其重命名为myFile.txt。这样做你只需忽略以.tmp结尾的文件...
Or, as already said, by alexcpn have a look at the Apache Camel File Component...
或者,正如已经说过的,由alexcpn看看Apache Camel文件组件......
#3
0
My approach would be to have the files written to a "temporary" directory first. After writing a file has finished, it should be moved to the final destination. Take care to have the "temporary" and the "final" directory on the same filesystem; otherwise, File.renameTo()
might or might not work. When using Java 7, Files.move()
might be of better use.
我的方法是首先将文件写入“临时”目录。写完文件后,应该将其移动到最终目的地。注意在同一个文件系统上有“临时”和“最终”目录;否则,File.renameTo()可能会也可能不会。使用Java 7时,可能更好地使用Files.move()。
Obviously, your application only has to check for files in the "final" directory, since you know all files in that directory are completely written.
显然,您的应用程序只需检查“final”目录中的文件,因为您知道该目录中的所有文件都已完全写入。
#4
0
In Windows there is no way to know when a file is locked for writing by another application, I've hit the same problem before. You can try using JNotify to listen for events and treat the exceptions that occur, as far as i know there is no other way, but in Unix checking if a file is locked should do the job new File(fileName).canWrite()
在Windows中,没有办法知道文件何时被另一个应用程序锁定以供写入,我之前遇到过同样的问题。您可以尝试使用JNotify监听事件并处理发生的异常,据我所知没有其他办法,但在Unix中检查文件是否被锁定应该执行作业新文件(fileName).canWrite()