How to test a text file is newly created?

W

www

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

Roedy Green

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" );
}
 
H

Hunter Gratzner

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

Roedy Green

I hope to set a check to see if the text file is newly generated. It
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
 
M

Mark Space

www said:
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.

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top