Read and Write to the File System

0
119

Adobe AIR provides you with the ability to read and write files to the file system. The following example will create a new file and then read it back.

Let’s review the code below. There are two TextArea and two Button components that make up this sample. The first TextArea (with an id of contents) will hold the contents of what is to be written to the file. The second (with an id of results) will output the file contents when read back. The application can be seen in Figure 5-2.

The File Save application

Clicking on the Button labeled Save will call the button1_clickHandler method. Within the button1_clickHandler method, an instance of File is created with the name file, the path is resolved to the userDirectory, and “samples/test.txt” is passed in to the resolvePath method. An instance of FileStream named stream is created to write the data to the file. The open method is then called on the stream object, and the file and FileMode.WRITE are passed in, which will open the file with write permissions. Next, the writeUTFBytes method is called and contents.text is passed in. Finally, the stream is closed. Figure 5-3 shows the new file within the File Manager application after it has been created.

Clicking on the Button labeled Load will call the button2_clickHandler method. Within the button2_clickHandler method, an instance of File is created with the name file, the path is resolved to the userDirectory, and “samples/test.txt” is passed in to the resolvePath method. An instance of FileStream named stream is created to read the data from the file. The open method is called on the stream object, and the file and FileMode.READ are passed in, which will open the file with write permissions. Next, the readUTFBytes method is called, the stream.bytesAvailable is passed in, and the results
are set to the results.text property of the second TextArea. Finally, the stream is closed. Figure 5-4 shows the contents of the file within the results TextArea:

<?xml version=”1.0″ encoding=”utf-8″?>
<s:Application xmlns:fx=”http://ns.adobe.com/mxml/2009″
xmlns:s=”library://ns.adobe.com/flex/spark”>
<fx:Script>
<![CDATA[
protectedfunction button1_clickHandler(event:MouseEvent):void
{
var file:File = File.userDirectory.resolvePath
(“samples/test.txt”);
var stream:FileStream = new FileStream()
stream.open(file, FileMode.WRITE);
stream.writeUTFBytes(contents.text);
stream.close();
}
protectedfunction button2_clickHandler(event:MouseEvent):void
{
var file:File = File.userDirectory.resolvePath
(“samples/test.txt”);
varstream:FileStream = new FileStream()
stream.open(file, FileMode.READ);
results.text = stream.readUTFBytes(stream.bytesAvailable);
stream.close();
}
]]>
</fx:Script>
<fx:Declarations>
<!– Place non-visual elements (e.g., services, value objects) here –>
</fx:Declarations>
<s:TextArea id=”contents” left=”10″ right=”10″ top=”10″ height=”100″/>
<s:Button right=”10″ top=”120″ label=”Save” click=”button1_clickHandler(event)”/>
<s:Button left=”10″ top=”200″ label=”Load” click=”button2_clickHandler(event)”/>
<s:TextArea id=”results” left=”10″ right=”10″ top=”280″ height=”100″
editable=”false”/>
</s:Application>

 

A new file shown within File Manager

File contents loaded into the results TextArea