简体   繁体   English

在写入文件时读取文件

[英]reading a file while it's being written

I've read some posts on stackoverflow about this topic but I'm still confused. 我已经阅读了一些关于stackoverflow的帖子,但是仍然很困惑。 When reading a file that is currently being written in Java, how do you keep track of how many lines have actually been written so that you don't get weird read results? 当读取当前正在用Java编写的文件时,如何跟踪实际写了多少行,从而不会得到奇怪的读取结果?

EDIT: sorry, I should have mentioned that the file writing it is in C++ and the one reading it is in Java so variables can't really be shared easily 编辑:对不起,我应该提到写它的文件是用C ++编写的,而读它的文件是用Java编写的,因此不能真正轻松地共享变量

When reading a file that is currently being written in Java, how do you keep track of how many lines have actually been written so that you don't get weird read results? 当读取当前正在用Java编写的文件时,如何跟踪实际写了多少行,从而不会得到奇怪的读取结果?

The problem is that you can never be sure that the current last character of the file is the end of a line. 问题是您永远无法确定文件的当前最后一个字符是行尾。 If it is a line terminator, you are OK. 如果它是行终止符,则可以。 If BufferedReader.readLine() will interpret it as a complete line without a line terminator ... and weird results will ensue. 如果BufferedReader.readLine()将其解释为没有行终止符的完整行,则会出现奇怪的结果。

What you need to do is to implement your own line buffering. 您需要做的是实现自己的行缓冲。 When you get an EOF you wait until the file grows some more and then resume reading the line. 当您获得EOF时,您将等到文件增长更多,然后继续读取该行。

Alternatively, if you are using Java 7 or later, the file watcher APIs allow you to watch for file writes without polling the file's size. 另外,如果您使用的是Java 7或更高版本,则文件观察器API允许您在不轮询文件大小的情况下监视文件写入。


By the way, there is an Apache commons class that is designed for doing this kind of thing: 顺便说一句,有一个Apache commons类专门用来做这种事情:

http://commons.apache.org/io/api-2.0/org/apache/commons/io/input/Tailer.html http://commons.apache.org/io/api-2.0/org/apache/commons/io/input/Tailer.html

If I understand, the file is being written in C# in some process and another Java process wants to read it while it is being written. 如果我的理解,该文件被写在C#中的一些过程和另一个Java程序要读取它,而正在写的。

Look at File Monitoring section on the tail command here . 此处 查看 tail命令的“ 文件监视”部分。 But I want to warn you that when I used the cygwin tail on Windows recently to follow log files that were rolling over, it sometimes failed under heavy load. 但我想警告您,当我最近在Windows上使用cygwin尾部来跟踪正在滚动的日志文件时,有时在高负载下会失败。 Other implementations may be more robust. 其他实现可能更健壮。

To have a count of the number of lines, just keep a counter on the side that's doing the writing. 要计算行数,只需在进行书写的一侧保留一个计数器即可。

So, every time you write a line, increment a counter, and make that counter readable via a method, something like, public int getNumLinesWritten() 因此,每次您写一行代码时,都要增加一个计数器,并通过诸如public int getNumLinesWritten()类的方法使该计数器可读。

The obvious answer to me... Why not use a buffer? 对我来说显而易见的答案...为什么不使用缓冲区? Use a string or whatever you need. 使用字符串或任何您需要的字符串。 (You could use a list/array of strings if you want, one for each line maybe?) Append to the string just as you would write to the file, then instead of reading from the file, read from that string. (如果需要,可以使用字符串列表/数组,也许每行一个?)像写入文件一样追加到字符串,然后从该字符串读取而不是从文件中读取。 Would that work for you? 那对你有用吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM