File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Java in General and the fly likes reading a dynamic file... Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "reading a dynamic file..." Watch "reading a dynamic file..." New topic

reading a dynamic file...

satya kiran
Ranch Hand

Joined: Nov 07, 2000
Posts: 52
I am finding difficulty to read a file created dynamically with the time stamp.
Here is the scenario:
I am having a directory called C:/infiles. The data files from other systems (different group) will be written to this directory.
The format of the file name will be develop_in_ [timestamp].dat
ie., develop_in_20030726122423.dat.
They will be dumping the latest files every time interval say 30 mts or so.. with the latest time stamp.
I need to read the file and change the name as PROCESSED_develop_in_20030726122423.dat, and update the database with this information.
How can i read the file as the name is not static (file name changes with each time stamp and i cannot figure out the time stamp).
Please suggest me how can i achieve this? This is very crucial part of my project.

Thanks in advance for your help,
Joel McNary

Joined: Aug 20, 2001
Posts: 1824

Check out the javadoc; particularly, look at the listFiles method. You simply need to write a Filename filter to get this to work:

That will return all the files in the directory that start with "develop_in_". If you need to know the actual timestamp used, you can, of course, parse it from the individual files' names.
[ July 28, 2003: Message edited by: Joel McNary ]

Piscis Babelis est parvus, flavus, et hiridicus, et est probabiliter insolitissima raritas in toto mundo.
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
We've had some other discussions about this with FTP and other processes on the same machine ...
A file may appear in the list as soon as the other program or system opens it to write it. And it may not yet be complete and closed and ready to read. Be prepared to handle an open failure, maybe by waiting until the next directory sweep. Another common trick is for the other system to write two files - the one you really want plus a second dummy file. When the dummy file exists, you know the real file is closed and ready to read.

A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
VIJAY Yadlapati
Ranch Hand

Joined: Aug 04, 2003
Posts: 175
As the File to be processed will be in the format "develop_in_XXXX", use for getting all files in c:\inFiles, and calculate the timestamp of every file starting with "develop_in_" using String.substring(11) (11 being length of "develop_in_" ) and converting the substring to Integer.
Process the file with least TimeStamp first and rename it later.
I agree. Here's the link:
subject: reading a dynamic file...
It's not a secret anymore!