Link to home
Start Free TrialLog in
Avatar of sajupk
sajupk

asked on

Java - Continuously Polling a text file for a new line

Hi

I have a text file which will be updated regularly by another program. The text file
looks like the following
-----------start of file (this is not from the file :) )---------------
abcd,asdfasdfasdfasdf
dfdfasdfas,asdfasf
-----------end of file ------------------------------------------------

I need to continuously poll this file from a java program and whenever the text
file is updated with a new line, i need to pick that line and process the content.
How can I do this using java?

Has any one tried some thing similar? Please let me know your suggestions.

Rgds
Saju
Avatar of girionis
girionis
Flag of Greece image

 Assuming that noone else is touching this file (apart from the two Java applications) you can use the lastModified() method to see whether the file was updated or not.

  For continuously polling the file you need a thread that will be checking the last modified date every n minutes. If it is changed then open the file and read the data, otherwise sleep for n amount of minutes and start over.
Avatar of wide_awake
wide_awake

what type of system will you be running this on?

If it's a unix-like system, you could make a line-parser, then create a simple shell script to do something like this:

tail -f <inputFile> | java LineParser
Avatar of sajupk

ASKER

I'm using unix, but if I use tail -f ....

will the jvm be invoked each time when there is a new entry in the file? If that is the case, then it wont be feasible to me because of performance overhead.

There are only two applications which are updating this
file, 1. my own reader and 2. another script which writes to the file.

So As per the suggestion I can use lastModified() in a thread to find whether there is a change or not.. but how can I read the new line alone from the file, ie. every time the other app updates the file with a new line, I should be able to read only that line, and not the old entries which I had allready processed.

How can I do this? If anyone has any sample code, please quote that.

Thanx
Saju

 You will still have to read the whole file but only keep the last line since the rest will already be processed. What I suggest is you have a private static int counter which counts lines you read. Every time you read a line you increment the counter. Next time you read the file you consult the counter. If the counter is equal to the current line you are using then the next one is the line you want to process. Read it and increment counter again.
how frequently is the text file updated?
Also, what sort of processing needs to be done on each new line?
Avatar of sajupk

ASKER

Frequency of file updation is like 10000 entries per hour.

The processing is some thing like i need to send a mail with that content, or some thing similar which might take few milli seconds.. let say 100 milliseconds per entry.

And it is a concern for me that I will have to read the entire file everytyime until the line number is greater than my counter. Is there a better solution than this? With which I can just directly go to the new entries..

It is okay for me even if delete the lines of the file after every read. If I go for this option the point to be kept in mind is that the app(a script) which writes into this file will be updating the file concurrently as the reader is reading the file for new entries. Hence we need to manage the file updation carefully from the reader java program.
 You can always synchronize access to the file.
ASKER CERTIFIED SOLUTION
Avatar of wide_awake
wide_awake

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of sajupk

ASKER

Hi Mark

Yup this works perfect! It doesnt invoke the jvm again and again, since the system.in.read() is waiting for the input and the main() will be invoked only once.

Any way there is no system.in.readLine() in InputStream :). Just kidding.

Thanx a lot for solving my head ache!

-Saju
Glad to help :)