> I realize this is a clunky-shell scripting way of doing things.
> Is there a more Java-esque way of doing this?
>
> Could I bypass the "backup the original file" part?
>Do whatever you need to with the temp file, and it will be automatically
>removed from your filesystem when the program terminates (when the JVM
>exits).
There is a problem with this. If your app crashes, then they won't be
deleted. If you have not given them some distinctive extension or
name, you can't very well run a file-deleting filter to find them
later and kill them.
So, I'd say in general, delete them right after you close them. That
does not eliminate the problem, but it reduces it.
--
Canadian Mind Products, Roedy Green.
Coaching, problem solving, economical contract programming.
See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
Harald Hein - 15 Sep 2003 22:58 GMT
> There is a problem with this. If your app crashes, then they
> won't be deleted.
This depends on the operating system. It is trivial to get this working
under Unix.
Roedy Green - 15 Sep 2003 23:38 GMT
>This depends on the operating system. It is trivial to get this working
>under Unix.
what I do is use a .tmp extension on my temps, and then run Batik each
day which scans my disk for .top files and kills them. It also
totally empties various caches. The problem is you have to customise
the list of directories to scan for every machine.
--
Canadian Mind Products, Roedy Green.
Coaching, problem solving, economical contract programming.
See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
Roedy Green - 16 Sep 2003 00:34 GMT
>This depends on the operating system. It is trivial to get this working
>under Unix.
What's the trick on Unix to ensuring temp files get deleted?
--
Canadian Mind Products, Roedy Green.
Coaching, problem solving, economical contract programming.
See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
Harald Hein - 16 Sep 2003 06:32 GMT
> What's the trick on Unix to ensuring temp files get deleted?
If we ignore Java for a moment you do the following: You open a file,
you get and keep a file handle, but you immediately unlink (remove) the
file. Due to the unlink it is gone from the directory. Due to Unix's
way of handling files, the file handle is still vaid, the file still
exists (it just doesn't have a directory handle) and the process
holding the file handle can read and write to it at will.
Unix keeps the file until a process has an open handle to it. When you
exit the process or the process crashes, the file gets closed. Since it
has no directory entry, Unix removes the file at that point.
That's the good news. The bad news is you have to be very lucky to get
this behaviour in Java because of the thick layer Java-IO has wrapped
around the necessary Unix system calls.
Steve Bowman - 19 Sep 2003 14:22 GMT
In a multi-threaded world:
I generally have a cleanup task present in most of my applications. It
is simply spawned off as a separate thread and watches over what
everything else is doing. It generally cleans up after other threads
collapse for what ever reason. When handling files, Java tends to
screw up occasionally, so the clean-up step is quite important.
The suggestion way back of having a new file created and then moving
it in to the correct place is good. This way other applications won't
complain when they see an incomplete/wierd file.
My 2 cents.
> > What's the trick on Unix to ensuring temp files get deleted?
>
[quoted text clipped - 12 lines]
> this behaviour in Java because of the thick layer Java-IO has wrapped
> around the necessary Unix system calls.
Mike Baranczak - 22 Sep 2003 23:53 GMT
> >Do whatever you need to with the temp file, and it will be automatically
> >removed from your filesystem when the program terminates (when the JVM
> >exits).
>
> There is a problem with this. If your app crashes, then they won't be
> deleted.
Incorrect. If your app crashes due to an uncaught exception, the JVM
will still clean up your temp files. (That's the way it works with
Apple's latest JRE; I'd assume that others work the same, but I could be
wrong.)
If the JVM itself crashes, then that's different; but crashing the JVM
is usually pretty damn difficult, unless you're working with buggy JNI
code.

Signature
http://macbuild.sourceforge.net/