G
Gordon Beaton
I have this app where I try to ftp to a box and read a file that is
actively being written to. After reading the first forty or fifty
lines a call to in.avaiable() on the InputStream even though the
file file that is accessed is still being written to. I am wondering
if there is a way of making the InputStream realize that there is
more data to be read.
Not really.
When you open the file, data structures (in the kernel or in libc, I'm
really not sure which) are initialized with the size of the file at
that particular moment. That information doesn't change even though
the file does, so you will get EOF at *that* point and you can't read
past it without closing the file and reopening it.
To continue reading from a known point that isn't the start of the
file, you should use a RandomAccessFile instead. Make a note of how
far you got, close and reopen the file, then seek to that position to
begin reading.
BTW your method of testing for EOF is flawed. If you have read the
last byte of the file, then all subsequent calls to available() will
return 0, "read" will never be assigned -1 and you will continue to
loop forever. In general, using available() makes it more difficult to
read from a stream because of that, and it gains you nothing. Simply
call read(). It will wait until there is something to read and return
EOF (-1) when the end of the stream has been reached. There will no
longer be any need for Thread.sleep() either.
I wonder if you've considered how you intend determine when the file
has finally stopped growing (EOF won't look any different).
/gordon