posted 21 years ago
The avilable() method does not necessarily give you all the bytes that are in a file; it gives you the number of bytes that at available at that moment (without waiting). For various reasons, the whole contents of a file may not be available for reading all at once. Maybe the file is fragmented into several pieces at different locations on the hard drive. Or maybe part ofthe system has an internal buffer that is full at the moment, and more bytes can't be read until the currently buffered bytes are cleared. Regardless, to know whether you have really reached the end of the file or not, you need to try to read at least one byte past the amount available(). If the method returns a -1, that indicates an end of file; otherwise, you just wait until more bytes are available. In fact, available() is pretty useless for reading files, since you can never detect an end of file unless you ignore it. Try something like this:
Each time through the loop, you will read either the number of bytes that are available, or the number of bytes that can fit in the buffer, whichever is less. Except the last time, when no bytes are available and you read anyway - the -1 tells you that you're done.
[ July 18, 2002: Message edited by: Jim Yingst ]
"I'm not back." - Bill Harding, Twister