我得到了解决方案的工作原理:
private boolean isCompletelyWritten(File file) {
RandomAccessFile stream = null;
try {
stream = new RandomAccessFile(file, "rw");
return true;
} catch (Exception e) {
log.info("Skipping file " + file.getName() + " for this iteration due it's not completely written");
} finally {
if (stream != null) {
try {
stream.close();
} catch (IOException e) {
log.error("Exception during closing file " + file.getName());
}
}
}
return false;
}
感谢@cklab和@Will以及所有其他建议以“独家锁定”方式寻找的人。我只是在这里发布代码,让其他人对人们使用它感兴趣。我相信@tigran建议的重命名解决方案也可以工作,但纯Java解决方案对我来说更可取。
附言:最初我用而不是,但它锁定了正在写入的文件。FileOutputStream
RandomAccessFile