The Scenario: A file is being downloaded from Internet .An application tries to read the same file , while it is being downloaded .How can that application check that this file is already opened in write mode.
Platform: Android
You can check that the file is being written to by polling the modification date. Once the file hasn't been modified for a while you might assume it has downloaded. Otherwise, there is no reliable way to do this without reading the file to see if it is complete.
我假设问题是,您正在阅读文件,但您没有获得end of file
令牌,您想知道该文件何时完成,即何时获得end of file
令牌。
现在有 2 个选项可用:
您知道正在下载文件的进程,在这种情况下,可以与该进程同步。这似乎不太可能,因为您可能不知道该文件。它可以是用例中的任何随机文件。
如果不是第一个选项,您只能进行错误处理。可能等待eof
令牌一段t
时间,在计时器到期后,再次读取文件的大小,如果它增加了,这意味着读取更多字节并再次启动相同的计时器进程。缺点是它可能需要无限的时间,所以你必须有一些机制来在两者之间的任何时间突然中断它。
这个问题似乎没有明确的答案。这完全取决于您的用例。唯一的选项似乎检查修改日期(如@Peter 提到的)或在运行时读取文件大小,然后根据您的用例进行处理。