Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 319
  • Last Modified:

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
0
sajupk
Asked:
sajupk
  • 5
  • 3
  • 3
1 Solution
 
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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
 
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

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 5
  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now