将评论组装成答案。
Easiest way to parse the files in the correct order is to load the entire directory file listing into an array / list and then sort the list using an appropriate comparator. E.g. Load files with File.list() or File.listFiles().
This is not the most efficient methodology, but for less than 10,000 files should be adequate unless you need faster startup time performance (I can imagine a small lag before processing begins as all of the files are listed).
为避免读取不完整的文件,您应该获取文件的独占锁FileLock(通过 aFileChannel您可以从FileOutputStreamor获取FileInputStream,但是您可能无法从 获取独占锁FileInputStream)。假设正在使用的操作系统支持文件锁定(现代操作系统支持)并且写入文件的应用程序表现良好并持有锁(希望如此),那么一旦您能够获得锁,您就知道文件已完成。
如果由于某种原因您不能依赖文件锁定,那么您需要让编写程序首先写入临时文件(可能具有不同的扩展名),然后自动移动/重命名文件(如果在同一个文件上,对于大多数操作系统来说是原子的)系统/分区),或监视文件一段时间以查看是否正在写入更多字节(不是最强大的方法)。