Question:
Continuous file reading?
Virus
2008-02-21 01:28:35 UTC
Hi everybody,

Is it possible to do that in JAVA: I have a text file that is filled
with new lines of text every n seconds. This text file get very massive.

I want to have my program to monitor this text file and to be able to
read only the new added line when it detect that a new line has been added.

I don't want to read the whole file every time, this would be high time
consuming.

Any idea? Thank you very much!
Five answers:
anonymous
2008-02-21 01:59:24 UTC
Well, if you use a FileReader object you get two convenience methods from Reader - mark() and reset(). Mark marks the end of the last bit read and reset resets the pointer to that point. So, if you read some data to the end and then mark it, you then write more data, reset to where you left off, and then go from there. That way, you don't start over.



The problem is how to notify your reader object that new data is there. The easiest way I can quickly think of is to create a separate read thread which reads and then waits indefinitely on an object. After each write, your write thread calls a notifyall on the wait object which wakes the read thread.
?
2008-02-21 04:07:49 UTC
Make the monitor program clear the text file after it reads the new lines out. That way the file will only contain the new lines since the monitor last read it. If you need all the lines in one file the monitor can add them all to a different file.



I assume you monitor program will be watching the file's timestamp? Thats what I'd do in C, I don't know enough about Java to say if you can do the same.
lao
2016-11-29 06:26:28 UTC
i spotted you're iterating over FileOpen and analyzing from it with the readlines() device which atleast in Python 2 would desire to provide you an errors asserting: "ValueError: mixing technology and consider procedures would lose information". additionally the readlines technique returns a itemizing of each and every of the lines interior the record. additionally, you're using the 'w' (write mode) to open the record even nevertheless your simplest analyzing from it that's undesirable save on with and misleading. ultimately, i'm now no longer particular what the variables TeamGK and TeamRB are in spite of the undeniable fact that they are generally international, so i'm now no longer particular what the reason of the final 2 lines are in spite of the undeniable fact that they do purely no longer look suited. one greater ingredient is, features are lowercase with words separated by underscores. type names are capwords. that's the Python convention and the different variety would be complicated to a Python programmer. i'd have have been given to be sure greater of your code to advise a suited restoration. If which you would be waiting to post the full script in pastebin and positioned a link interior the greater info.
dippu75
2008-02-21 09:37:55 UTC
I have done this through VB. But concept is similar, and as "satsumo" described:

Just clear the contents when reading the file.

And when you read it again, ignore if its blank.

If you get modification date & time stamp then you can remember (store) last reading stamp and update only when modification happens.

You didn't described that "who" fills your monitoring file?

You can also delete old file after reading and let that file be created again.
Invasion!
2008-02-21 01:34:25 UTC
there is no such program it is imposibble. plus, can i hav my answer chosen as the best plz?


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Continue reading on narkive:
Loading...