Reading and Writing Files in AIR – using a simple text editor
January 26th, 2009Monstagon asked me if I ever saved or opened files in Adobe AIR. Yes. I have. I made a simple text editor to demonstrate the basics of reading files in (letting the user browse for a file on their machine) and writing a save (also letting the user specify where to save the new file).
Some issues I encountered with making the text editor were the different FileModes (READ, WRITE, UPDATE, and APPEND). UPDATE and APPEND basically just add whatever you write to the FileStream to the end of the file. WRITE deletes the destination and writes only what is sent to the FileStream. READ only opens the file for reading. After erasing some files accidentally by opening it with WRITE, I realized for the text editor to work, first READ the file in, close the FileStream. When the user hits Save, open the file in WRITE mode, and send the contents of the editor to the FileStream.
I also had to removeEventListener from the FileStream because it was listening for two Event.SELECT’s: user selects which file to open, and user selects where to save. This was throwing errors when the user selects where to save and the program tries to open a file that doesn’t exist.
If you just want to know how to read a file, look at private function Open() and follow the code. For writing, private function Save(). Note that in ActionScript, its all about event listeners. So reading the file contents into the TextArea is 3 function, because we have to wait until each step is ready. Else, if you don’t setup the Event Listeners nothing will happen.
<mx:WindowedApplication xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute">
<mx:Script>
<![CDATA[
import mx.events.FileEvent;
private var stream:FileStream;
private var file:File = File.documentsDirectory;
private var newFile:Boolean = true;
private function New():void
{
newFile = true;
editor.text = '';
}
private function Open():void
{
try
{
file.browseForOpen("Open");
file.addEventListener(Event.SELECT, LoadFile);
}
catch (error:Error)
{
trace("Failed:", error.message);
}
}
private function LoadFile(evt:Event):void
{
file.removeEventListener(Event.SELECT, LoadFile);
file = evt.currentTarget as File;
stream = new FileStream();
stream.addEventListener(Event.COMPLETE, ReadFile);
stream.openAsync(file,FileMode.READ);
}
private function ReadFile(evt:Event):void
{
stream.removeEventListener(Event.COMPLETE,ReadFile);
var str:String = stream.readUTFBytes(stream.bytesAvailable);
editor.text = str;
newFile = false;
stream.close();
}
private function Save():void
{
if(!newFile)
{
stream = new FileStream();
stream.openAsync(file,FileMode.WRITE);
stream.writeUTFBytes(editor.text);
stream.close();
}else
{
file.browseForSave("Save");
file.addEventListener(Event.SELECT, SaveFile);
}
}
private function SaveFile(evt:Event):void
{
file.removeEventListener(Event.SELECT, SaveFile);
var file:File = evt.currentTarget as File;
stream = new FileStream();
stream.openAsync(file,FileMode.WRITE);
stream.writeUTFBytes(editor.text);
stream.close();
newFile = false;
}
]]>
</mx:Script>
<mx:VBox width="100%" height="100%">
<mx:HBox>
<mx:Button label="New" click="New();" />
<mx:Button label="Open" click="Open();" />
<mx:Button label="Save" click="Save();" />
</mx:HBox>
<mx:TextArea id="editor" width="100%" height="100%" />
</mx:VBox>
</mx:WindowedApplication>
