?
Solved

Java - Continuously Polling a text file for a new line

Posted on 2003-03-24
11
Medium Priority
?
304 Views
Last Modified: 2010-03-31
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
Comment
Question by:sajupk
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 3
  • 3
11 Comments
 
LVL 35

Expert Comment

by:girionis
ID: 8194939
 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
 
LVL 3

Expert Comment

by:wide_awake
ID: 8197834
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
 

Author Comment

by:sajupk
ID: 8200560
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
Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

 
LVL 35

Expert Comment

by:girionis
ID: 8201387
 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
 
LVL 3

Expert Comment

by:wide_awake
ID: 8204015
how frequently is the text file updated?
0
 
LVL 3

Expert Comment

by:wide_awake
ID: 8204030
Also, what sort of processing needs to be done on each new line?
0
 

Author Comment

by:sajupk
ID: 8209201
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
 
LVL 35

Expert Comment

by:girionis
ID: 8209228
 You can always synchronize access to the file.
0
 
LVL 3

Accepted Solution

by:
wide_awake earned 200 total points
ID: 8211630
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
 

Author Comment

by:sajupk
ID: 8216132
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
 
LVL 3

Expert Comment

by:wide_awake
ID: 8218919
Glad to help :)
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
Suggested Courses
Course of the Month11 days, 23 hours left to enroll

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question