如何读取/复制一个(部分锁定的)日志文件,或者至少是未锁定的零件?
我正在开发一个解散许多文件(用于诊断目的)的实用程序。它的核心使用以下功能:
private void write(ZipOutputStream zipStream, String entryPath, ByteSource content) throws IOException {
try (InputStream contentStream = content.openStream()) {
zipStream.putNextEntry(new ZipEntry(entryPath));
ByteStreams.copy(contentStream, zipStream);
zipStream.closeEntry();
}
}
但是我要读取的文件之一是另一个应用程序运行和锁定的日志文件。因为该文件已锁定,所以我得到了一个IO例外。
<ERROR>java.io.IOException: The process cannot access the file because another process has locked a portion of the file
at java.base/java.io.FileInputStream.readBytes(Native Method)
at java.base/java.io.FileInputStream.read(FileInputStream.java:257)
at com.google.common.io.ByteStreams.copy(ByteStreams.java:112)
如果我愿意接受我的阅读和其他应用程序写作之间的冲突,我可能会造成一些垃圾,那么解决此问题的最佳/最简单方法是什么?是否有一个文件读取器忽略锁,或者也许仅读取所有未锁定的部分?
更新 - 要澄清,我希望阅读日志文件或尽可能多的日志文件。因此,我可以开始读取文件,等到我无法阅读的块,捕获错误,附加文件结束然后进行。记事本++和其他程序可以读取部分锁定的文件。我只是在寻找一种方法来做到这一点,而无需重新发明字节函数。复制函数以创建“尽可能多的复制”功能。
我应该问“如何读取日志文件的所有未锁定部分”,然后我将更新标题。
I am working on a utility that zips up a number of files (for diagnostics purposes). At it's core, it uses the following function:
private void write(ZipOutputStream zipStream, String entryPath, ByteSource content) throws IOException {
try (InputStream contentStream = content.openStream()) {
zipStream.putNextEntry(new ZipEntry(entryPath));
ByteStreams.copy(contentStream, zipStream);
zipStream.closeEntry();
}
}
But one of the files I want to read is a log file that another application runs and locks. Because that file is locked, I get an IO exception.
<ERROR>java.io.IOException: The process cannot access the file because another process has locked a portion of the file
at java.base/java.io.FileInputStream.readBytes(Native Method)
at java.base/java.io.FileInputStream.read(FileInputStream.java:257)
at com.google.common.io.ByteStreams.copy(ByteStreams.java:112)
If I am willing to accept that I might get some garbage because of conflicts between my reads and the other application's writes, what is the best/easiest way to work around this? Is there a file reader that ignores locks or perhaps only reads all the unlocked sections only?
Update -- To clarify, I am looking to read a log file, or as much of it as possible. So, I could just start reading the file, wait until I get a block I can't read, catch the error, append a file end and go. Notepad++ and other programs can read files that are partially locked. I'm just looking for a way to do that without re-inventing the ByteStreams.copy function to create a "Copy as much as I can" function.
I should have perhaps asked "How to read all the unlocked parts of a log file" and I will update the title.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
一个可能的答案(我不喜欢)是创建一种与bytestreams.copy()几乎相同的方法,我称之为“ copyuntillock”,它捕获了任何ioexception,然后检查以查看是否例外是因为另一个过程有另一个过程锁定文件的一部分。
如果是这种情况,那么只需停止写作并返回到目前为止的字节数。如果还有其他例外,请继续进行。 (您还可以为流写一张注释,例如“由于锁定而失败”)。
仍在寻找更好的答案。代码下面包括。
One possible answer (which I don't like) is to create a method almost identical to ByteStreams.copy(), which I call "copyUntilLock" which catches any IOException, then it checks to see if the exception is a because another process has locked a portion of the file.
If that is the case, then simply stop writing and return the number of bytes so far. If its some other exception go ahead and throw it. (You could also write a note to the stream like "READING FAILED DUE TO LOCK").
Still looking for a better answer. Code included below.