From: Daniel Pitts on 4 Dec 2009 11:22 Lothar Kimmeringer wrote: > markspace wrote: > >> Good idea. Another in the same vein, if the stream is a network stream >> that can't be re-opened (easily), then make a temp file and copy it to >> the temp file. Then you can re-open the temp file as random access. >> >> File.createTempFile() I think is the name of the method to create a >> temporary file. > > It is but the file is not a temporary file that gets deleted > automagically. You have to do that for yourself, otherwise your > temporary folder get flooded by files over time. > > > Regards, Lothar Unless you call "file.deleteOnExit()", then it will get deleted automagically (except on JVM crash). -- Daniel Pitts' Tech Blog: <http://virtualinfinity.net/wordpress/>
From: Lothar Kimmeringer on 4 Dec 2009 18:30 Daniel Pitts wrote: [File.createTempFile()] > Unless you call "file.deleteOnExit()", then it will get deleted > automagically (except on JVM crash). .... and on long running server processes. Regards, Lothar -- Lothar Kimmeringer E-Mail: spamfang(a)kimmeringer.de PGP-encrypted mails preferred (Key-ID: 0x8BC3CD81) Always remember: The answer is forty-two, there can only be wrong questions!
From: Daniel Pitts on 4 Dec 2009 19:41 Lothar Kimmeringer wrote: > Daniel Pitts wrote: > > [File.createTempFile()] > >> Unless you call "file.deleteOnExit()", then it will get deleted >> automagically (except on JVM crash). > > .... and on long running server processes. > > > Regards, Lothar True, ideally you would have: try { handleTempFile(file); } finally {file.delete();} -- Daniel Pitts' Tech Blog: <http://virtualinfinity.net/wordpress/>
From: Tom Anderson on 5 Dec 2009 05:11 On Fri, 4 Dec 2009, Daniel Pitts wrote: > Lothar Kimmeringer wrote: >> Daniel Pitts wrote: >> >> [File.createTempFile()] >> >>> Unless you call "file.deleteOnExit()", then it will get deleted >>> automagically (except on JVM crash). >> >> .... and on long running server processes. > > True, ideally you would have: > try { handleTempFile(file); } finally {file.delete();} Although this still doesn't handle crashes. I think there is a trick you can do on unix to have files deleted even when the process crashes - something like create, open, then delete the directory entry, so that the only reference keeping the file alive is from the open filehandle, which will die when the process exits - but i don't know if there's a way to use it from java. Or even that this is definitely correct. However, by default, createTempFile puts files in java.io.tmpdir, which on unix machines will typically be /tmp. Files there are subject to deletion at the whim of the OS, so to an extent, you can delegate the problem of worrying about deleting files to that. That said, i'm not sure what current unixen's policies towards /tmp are; i believe linux will only delete things at reboot, not during normal operation, which makes this less useful. I used a system (OSF/1?) at some point that had a /scr, for scratch, which was deleted more aggressively, which would be ideal for this. tom -- .... to build a space elevator, that's got to be hundreds of thousands of pounds ... -- Mike Froggatt
From: Martin Gregorie on 5 Dec 2009 16:51
On Sat, 05 Dec 2009 10:11:30 +0000, Tom Anderson wrote: > Although this still doesn't handle crashes. I think there is a trick you > can do on unix to have files deleted even when the process crashes - > something like create, open, then delete the directory entry, so that > the only reference keeping the file alive is from the open filehandle, > which will die when the process exits - but i don't know if there's a > way to use it from java. Or even that this is definitely correct. > Thats correct. Its the standard UNIX idiom for making sure that temporary files don't outlive the process that created them no matter how it dies. It should work from Java since its not language-dependent, though of course its not portable outside outside the *nix world. > However, by default, createTempFile puts files in java.io.tmpdir, which > on unix machines will typically be /tmp. Files there are subject to > deletion at the whim of the OS, so to an extent, you can delegate the > problem of worrying about deleting files to that. > You should attempt to delete them at some stage because there's no guarantee that the OS will. Its merely a way of guaranteeing that the tempfile has a unique name no matter how many copies of the process are running. A more useful approach would be to start the process(es) from a shell script or control process whose first action is to delete all temporary files it finds that are used by the processes it controls: this will be portable provided the script/control process is portable: no reason it shouldn't be written in Java or a portable scripting language like Groovy or Python. > That said, i'm not sure what current unixen's policies towards /tmp are; > i believe linux will only delete things at reboot, not during normal > operation, which makes this less useful. > I'm not certain that temp files are necessarily deleted at boot because that does slow down crash recovery. Since a file in temp will survive until its closed, its equally likely that there's a cron job that runs 'rm -rf /tmp/*' sometime after midnight each day. The real caveat is that no program creating files in /tmp should expect them to be there after it terminates, i.e. don't pass them to another program started after the first ends. -- martin@ | Martin Gregorie gregorie. | Essex, UK org | |