A
Anabolik
How to return to the begin of InputStream after reading this file?
Anabolik said:How to return to the begin of InputStream after reading this file?
Anabolik said:How to return to the begin of InputStream after reading this file?
Lothar said:use a BufferedInputStream with mark and reset or just reopen
the FileInputStream. If you need to access the file in a
"random" (non-sequential) way, you can use RandomAccessFile
http://java.sun.com/j2se/1.5.0/docs/api/java/io/RandomAccessFile.html
and call seek(0)
Arne said:Lothar said:Anabolik said:How to return to the begin of InputStream after reading this file?
use a BufferedInputStream with mark and reset [...]
I would not rely on mark reset.
The docs for the marklimit method does not give a comfortable
feeling.
If it is a FileInputStream, you can use mark/reset, or you can re-openAnabolik said:How to return to the begin of InputStream after reading this file?
Arne said:Close and reopen the file.
How to return to the begin of InputStream after reading this file?
Anabolik said:How to return to the begin of InputStream after reading this file?
markspace said: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.
Lothar said: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).
True, ideally you would have:Lothar said: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 said: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();}
Thats correct. Its the standard UNIX idiom for making sure that temporaryAlthough 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.
You should attempt to delete them at some stage because there's noHowever, 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.
I'm not certain that temp files are necessarily deleted at boot becauseThat 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.
Martin said: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.
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.
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.
Good point.Also, it's a *very* bad idea to purge /tmp blindly, even if
you're careful only to purge files that haven't been modified in a
while. I recall working with a server application that put files in
/tmp and mmap'ed them to share memory between its multiple processes.
Since simple paging I/O to and from a file opened a week ago doesn't
change the files' modification date, along came the customer's
/tmp-purging cron job and BLOOEY went the server ...
(Marginally topical) On Solaris, the Unix flavor I'm most familiar
with, /tmp is usually mounted on a tmpfs file system. This is a
memory-resident file system to the extent possible, spilling over into
swap space as needed. Nothing special needs to happen at reboot to
"clean out" tmpfs, no more than anything special needs to happen to
"clean out" swap files: The newly- booted system just initializes its
metadata to say "empty," and everything from prior incarnations is gone.
Also, it's a *very* bad idea to purge /tmp blindly, even if you're
careful only to purge files that haven't been modified in a while. I
recall working with a server application that put files in /tmp and
mmap'ed them to share memory between its multiple processes. Since
simple paging I/O to and from a file opened a week ago doesn't change
the files' modification date, along came the customer's /tmp-purging
cron job and BLOOEY went the server ...
Some non-*nix OSes (OS/400, VMS?) refuse to accept the delete() operationHang on, the files were open, right? So how could they be deleted?
This is what I was muttering about. In the *nix family, which includesOr is the point that the directory entries were deleted, so when new
processes were spawned, they couldn't open the file?
Works OK for Linux and most *nixen, don't know about others.And since when did writing to
a file via an mmap not change its modification time, anyway?
Exactly so.Either way, i'd suggest the bad idea here was putting critical
long-lived files in /tmp. Yes, they're temporary, but not that
temporary!
(Marginally topical) [...]Also, it's a *very* bad idea to purge /tmp blindly, even if you're
careful only to purge files that haven't been modified in a while. I
recall working with a server application that put files in /tmp and
mmap'ed them to share memory between its multiple processes. Since
simple paging I/O to and from a file opened a week ago doesn't change
the files' modification date, along came the customer's /tmp-purging
cron job and BLOOEY went the server ...
Hang on, the files were open, right? So how could they be deleted? Or is
the point that the directory entries were deleted, so when new processes
were spawned, they couldn't open the file? And since when did writing to
a file via an mmap not change its modification time, anyway?
Either way, i'd suggest the bad idea here was putting critical
long-lived files in /tmp. Yes, they're temporary, but not that temporary!
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.