Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / March 2005

Tip: Looking for answers? Try searching our database.

Efficient polling of a flatfile

Thread view: 
Ike - 04 Mar 2005 16:41 GMT
Just looking for feedback on what you might consider the most efficient
means to poll a flat file.

I need to have my app look at a flat file, say, every second or so, and read
any data that may have been appended to it.

Wondering what others might consider an efficient means of doing this in
Java. Would it be wise to merely look at the file size everysecond or so,
and, if that has changed, to then read it, starting from that byte which was
the previous size of the file on the previous polling a second ago?

Is there a more efficent way to do this? Thanks, Ike
Fahd Shariff - 04 Mar 2005 17:57 GMT
Hi,

An alternative approach might be to:

- Add a timer to your app.
- At each interval your app would check the last modified time i.e.
file.lastModified() to find out if it has changed.
- If there is a change it would notify all listeners.
- The listener would then read the appended text. If the text is
delimited you could use the substring method.

Otherwise your byte method looks fine to.

--
Fahd Shariff
http://www.fahdshariff.cjb.net
"Let the code do the talking... "
Dotty - 05 Mar 2005 01:24 GMT
> Hi,
>
[quoted text clipped - 13 lines]
> http://www.fahdshariff.cjb.net
> "Let the code do the talking... "

But is the resolution of the file time only one second?
Maybe check the length since others only append.
What do you do if you try to access the file when it is
being used by another process?
HK - 04 Mar 2005 22:18 GMT
> Wondering what others might consider an efficient means of doing this in
> Java. Would it be wise to merely look at the file size everysecond or so,
> and, if that has changed, to then read it, starting from that byte which was
> the previous size of the file on the previous polling a second ago?

Hopefully you really only want to read bytes. For any data type
which is longer than one byte you may run into the problem that
the new data ends in the middle of an item (int, char, etc).
This happens if you do not have 100% control over the output
buffering of the application which writes your file. Consequently
you must do your own buffering when reading to handle incomplete data
items at the end of the buffer. A likely trap is char data, because
some encodings are more than one byte long.

Sometimes the scheme you describe cannot be avoided, but if at
all possible, I would try to redirect or duplicate the output
of the program which generates the data so that the application
we are talking about can just read the data. If you are on a UNIX
like system, pipes, named pipes, tee come to mind. In general,
sockets are always an option.

 Harald.
Jacob - 05 Mar 2005 06:38 GMT
> Just looking for feedback on what you might consider the most efficient
> means to poll a flat file.
[quoted text clipped - 8 lines]
>
> Is there a more efficent way to do this? Thanks, Ike

You can check the "fileaccessor" module of http://geosoft.no/software
which does exactly this.

The moudle is generic, so it checks the lastModified tag only. What
you suggest is more specific as you imply knowledge of *how* your
source file changes with time. This might be important if the file is
large and/or performance is extremely important.

Signature

Jacob Dreyer
G - Free 2D Graphics Library for Java  - http://geosoft.no/graphics
Free Java Software                     - http://geosoft.no/software

Ike - 05 Mar 2005 14:02 GMT
Wow, nice set of classes at this site Jacob! I'm still concenerned about
what Dotty says regarding what might occur if a file is open in another
process when I go to read it. -Ike

> > Just looking for feedback on what you might consider the most efficient
> > means to poll a flat file.
[quoted text clipped - 16 lines]
> source file changes with time. This might be important if the file is
> large and/or performance is extremely important.


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.