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
sajupkAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

girionisCommented:
 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.
0
wide_awakeCommented:
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
0
sajupkAuthor Commented:
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

0
Cloud Class® Course: Microsoft Office 2010

This course will introduce you to the interfaces and features of Microsoft Office 2010 Word, Excel, PowerPoint, Outlook, and Access. You will learn about the features that are shared between all products in the Office suite, as well as the new features that are product specific.

girionisCommented:
 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.
0
wide_awakeCommented:
how frequently is the text file updated?
0
wide_awakeCommented:
Also, what sort of processing needs to be done on each new line?
0
sajupkAuthor Commented:
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.
0
girionisCommented:
 You can always synchronize access to the file.
0
wide_awakeCommented:
I don't have a unix box here right now to test this, but I *think* if you write a processing program that keeps reading lines from stdin, the JVM will only be invoked initially, then your java program will just wait until it gets info from tail -f.

for example:

public static void main(String[] args)
{
  String line;
  while((line = System.in.readLine()) != null)
  {
    processLine(line);
  }
}

It should work like a charm.

-Mark.

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
sajupkAuthor Commented:
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
0
wide_awakeCommented:
Glad to help :)
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.