Reputation: 11
I'm working with Flash Air 3.2 (Old, I know) and I'm trying to start off by reading a text file using FileStream. The code I have finds the file, and I've put text in the file, but whenever I try to run the code it returns 'undefined'. Here's my code:
import flash.filesystem.*
import flash.net.*
import flash.utils.ByteArray;
import flash.events.*
var myFile:File = File.applicationStorageDirectory.resolvePath("test.txt");
var myFileStream:FileStream = new FileStream();
myFileStream.addEventListener(Event.COMPLETE, completeHandler);
myFileStream.openAsync(myFile, FileMode.READ);
var bytes:ByteArray = new ByteArray();
function completeHandler(event:Event):void
{
myFileStream.readBytes(bytes, 0, myFileStream.bytesAvailable);
trace(myFileStream.readBytes(bytes, 0, myFileStream.bytesAvailable));
}
Any help is greatly appreciated!
Upvotes: 0
Views: 86
Reputation: 14406
You get undefined because the readBytes
method of a FileStream has a return type of void
. The bytes read from that method get put into the byte array you pass to it rather than being returned directly from the method.
myFileStream.readBytes(bytes, 0, myFileStream.bytesAvailable); //this returns void/undefined by design regardless of the contents of bytes
trace(bytes); //this will trace the byte array object that was populated with the above line
If you're just looking to read a text file, you should be able to do the shortcut method:
myFileStream.readUTF();
Upvotes: 1