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 / September 2007

Tip: Looking for answers? Try searching our database.

How to test a text file is newly created?

Thread view: 
www - 27 Sep 2007 19:54 GMT
Hi,

My program first generate a text file, then read it and process it. The
problem of my current program is that, if running failed, that text file
generated last run will be read and processed.

I hope to set a check to see if the text file is newly generated. It
it is not, that means it is generated by previous run and current run
has failed. The program then stop and will not proceed to read that file.

Could you show me how to do this? Thank you very much.
Roedy Green - 27 Sep 2007 22:43 GMT
>I hope to set a check to see if the text file is newly generated. It
>it is not, that means it is generated by previous run and current run
>has failed. The program then stop and will not proceed to read that file.

On windows you can get at its create timestamp with
http://mindprod.com/products1.html#FILETIMES

You can see if it exists with File.exists

       // set up handle to file name.
       File f = new File( "E:\\genus\\species\\crocodile.html" );

       // does the file exist
       if ( f.exists() )
           {
           System.out.println( "file exists" );
           }
Signature

Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com

Roedy Green - 28 Sep 2007 02:02 GMT
>>I hope to set a check to see if the text file is newly generated. It
>>it is not, that means it is generated by previous run and current run
>>has failed. The program then stop and will not proceed to read that file.

logic I use often is to.

1. test if I really need to recreate the file.

2. generate the new file in a temp file.

3. If it is different from the old file, delete the old file, rename
the new file to the old file.

4. If it is the same as the old file, delete the temp file.

You don't want to disturb a file to change its date without truly
changing its contents. That will trigger needless indexing, FTP
uploads, backups, defrags etc etc.

Sometimes I use UNTOUCH logic to put last modified dates back the way
they were.

See http://mindprod.com/products1.html#UNTOUCH
Signature

Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com

Hunter Gratzner - 28 Sep 2007 01:34 GMT
> Hi,
>
> My program first generate a text file, then read it and process it. The
> problem of my current program is that, if running failed, that text file
> generated last run will be read and processed.

Then your program logic if faulty. What prevents you to first trying
to delete any previously existing file? What prevents you from not
doing the processing if your file creation fails? Did you try to be
clever and are you ignoring exceptions?
Mark Space - 28 Sep 2007 04:47 GMT
> Hi,
>
[quoted text clipped - 7 lines]
>
> Could you show me how to do this? Thank you very much.

This is basically an unsolvable problem.  The best you can do is sort of
fake it.  Put enough wolf-fencing around the file and hopefully you will
never get to the point where the entire system fails because all of the
different checks go awry.

This is a transaction system similar to what many databases use.
There's several ways to solve this, I'm sure.  One would be to log all
transactions (files) and to have the other side also log all
transactions.  That way, if (and when) the system fails, you can
re-start and read the logs to determine where you left off.

You may also need to create some sort of ticket or cookie.  For example,
if two sides are communicating, you may want to create a ticket that
says "Processing-file-MyFile.txt-28/SEP/2007.18:34:23.42112-Line-1" then
process that line, then log "Completed-file-...etc..-Line-1" then log
"Processing-file-etc...-Line-2" and continue on that way until the whole
file is processed.

(Of course, recording lines processed may not be important.  Just choose
whatever sub-unit is appropriate.  If the files are simple and quick to
process, you could do them in one go.)

Then on the other end, record the ticket given, and when and if the
processing was completed.  Then when the system fails, it can be
restarted and the first bit will ask the second bit whether any parts
haven't got to the "completed" part yet, and if there are, restart from
there.


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



©2009 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.