Sometimes (okay, manytimes) files are large enough that the process of opening and reading its contents may take some time. To solve issues related to the extra time this may take AS3 allows you to open files asynchronously. I'd like to add to the information from the reading files with AS3 post. Here we'll cover the code necessary to open a file asynchronously and read the file contents as they become available.
Because we are now doing something asynchronously, we will be dealing with events. As the file contents are being read The FileStream object broadcasts progress events that we can listen for and respond to. This ensures that when we are reading data when the data is available.
So, onto the code:
-
// Imports
-
import flash.filesystem.FileMode;
-
import flash.filesystem.FileStream;
-
import flash.filesystem.File;
-
import flash.events.ProgressEvent;
-
import flash.events.Event;
-
-
// Declare the FileStream and String variables
-
private var _fileStream:FileStream;
-
private var _fileContents:String;
-
-
private function onCreationComplete():void // Fired when the application has been created
-
{
-
var myFile:File = File.appResourceDirectory; // Create out file object and tell our File Object where to look for the file
-
myFile = myFile.resolve("mySampleFile.txt"); // Point it to an actual file
-
-
_fileStream = new FileStream(); // Create our file stream
-
-
_fileStream.addEventListener(ProgressEvent.PROGRESS, onFileProgress); // Add our the progress event listener
-
_fileStream.addEventListener(Event.COMPLETE, onFileComplete); // Add our the complete event listener
-
-
_fileStream.openAsync(myFile, FileMode.READ); // Call the openAsync() method instead of open()
-
}
-
-
private function onFileProgress(p_evt:ProgressEvent):void // Event handler for the PROGRESS Event
-
{
-
_fileContents += _fileStream.readMultiByte(_fileStream.bytesAvailable, "iso-8859-1"); // Read the contens of the file and add to the contents variable
-
-
fileContents_txt.text = _fileContents; // Display the contents. I've created a TextArea on the stage for display
-
}
-
-
private function onFileComplete(p_evt:Event):void // Event handler for the COMPLETE event
-
{
-
_fileStream.close(); // Clean up and close the file stream
-
}
As for what all that code does - first off, import the required classes:
-
import flash.filesystem.FileMode;
-
import flash.filesystem.FileStream;
-
import flash.filesystem.File;
-
import flash.events.ProgressEvent;
-
import flash.events.Event;
Then we'll need to create a couple variables. First, the FileStream which we'll use to read our file and second, a String variable that we'll use to display the file contents.
-
private var _fileStream:FileStream;
-
private var _fileContents:String;
Now we'll create an onCreationComplete() method. This method will handle the creationComplete event of the application to get things rolling. Inside the onCreationComplete() method we'll need to create out File object as well as the FileStream object.
-
private function onCreationComplete():void // Fired when the application has been created
-
{
-
var myFile:File = File.appResourceDirectory;
-
myFile = myFile.resolve("mySampleFile.txt");
-
-
_fileStream = new FileStream();
-
}
Still in the onCreationComplete() method, we'll need to handle those events that will be dispatched while reading the file asynchronously. The events that we will handle are:
ProgressEvent.PROGRESS- This will fire as the bytes are read from the file into the buffer.Event.COMPLETE- This will fire when all the bytes of the file have been read into the buffer.
-
private function onCreationComplete():void // Fired when the application has been created
-
{
-
var myFile:File = File.appResourceDirectory;
-
myFile = myFile.resolve("mySampleFile.txt");
-
-
_fileStream = new FileStream();
-
-
_fileStream.addEventListener(ProgressEvent.PROGRESS, onFileProgress)
-
_fileStream.addEventListener(Event.COMPLETE, onFileComplete);
-
}
The final part of the onCreationComplete() method is to open the file asynchronously. Add the following line to the end of the method:
-
_fileStream.openAsync(myFile, FileMode.READ);
Now to handle those events and read our file. First, we'll deal with the PROGRESS event. Inside the onFileProgress() event handler method, we read the availableBytes from the file and add it to our String variable. Notice the second parameter of the readMultiByte() method - "iso-859-1". This specifies the content type of the file that we are reading. I'll cover some of the other content types in a later post. So, keep an eye out for that one if you're curious. Finally, we update the TextArea, to show we are actually reading the file.
-
private function onFileProgress(p_evt:ProgressEvent):void
-
{
-
_fileContents += _fileStream.readMultiByte(_fileStream.bytesAvailable, "iso-8859-1");
-
-
fileContents_txt.text = _fileContents;
-
}
We still need to clean up after ourselves, so in the onFileComplete() method, we'll need to close out FileStream object.
-
private function onFileComplete(p_evt:Event):void
-
{
-
_fileStream.close();
-
}
A very simple example, and it is still only text that we are reading. Next time I'd like to cover how to read different file types. So, like I said, keep an eye our for that post.
