Prev | Current Page 592 | Next

L. McColl-Sylvester and F. Ponticelli

"Professional haXe and Neko"

If you perform this method call within a while loop and such an event occurs,
the stream will never reach an end - of - file state; thus the loop will perpetuate until broken by other means:
class ReadBytes
{
public static function main()
{
var stream = neko.io.File.read( ???example.txt???, false );
var str = neko.Lib.makeString( 20 ); // 20 is the number of chars
while ( !stream.eof() )
{
try
{
stream.readBytes(str, 0, 20);
}
catch ( err : Dynamic )
{
neko.Lib.println( err );
}
neko.Lib.println( str );
}
stream.close();
}
}
Normally, when extracting bytes from a stream, you will need to check how many bytes were extracted
using the return value of readBytes . This way, you can make use of the number of bytes returned,
while avoiding the gobbledygook that preexisted in the string buffer. When finished with the stream,
always remember to close it, so as to release the file handle and free valuable resources.
readAll
You can actually go one better than this, now, using the readAll method.


Pages:
580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604