Just as in the desktop version of Adobe AIR, AIR on BlackBerry
Tablet OS provides you access to the file system. The usage is exactly the
same.
1. Folder Aliases
To access the file system, you can navigate using several folder
static alias properties of the File class. Since you
are attempting to read these shared files, you will need to select the
access_shared permission when
creating your project.
Let’s take a look at the following code. Within
applicationComplete of the application, the
application1_applicationCompleteHandler method is
called and the static File properties are read and written to a
String variable. This String
variable is written to the text property of a
TextArea component. Figure 1 shows the results. You will notice that
many of the aliases return the same value, which is a path to the
device’s external storage card.
<?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"
applicationComplete="application1_applicationCompleteHandler(event)">
<fx:Script>
<![CDATA[
import mx.events.FlexEvent;
protected function application1_applicationCompleteHandler
(event:FlexEvent):void
{
var s:String = "";
s += "File.applicationDirectory : " +
File.applicationDirectory.nativePath + "\n\n";
s += "File.applicationStorageDirectory : " +
File.applicationStorageDirectory.nativePath + "\n\n";
s += "File.desktopDirectory: " +
File.desktopDirectory.nativePath + "\n\n";
s += "File.documentsDirectory : " +
File.documentsDirectory.nativePath + "\n\n";
s += "File.userDirectory : " +
File.userDirectory.nativePath + "\n\n";
info.text = s;
}
]]>
</fx:Script>
<fx:Declarations>
<!-- Place non-visual elements (e.g., services, value objects) here -->
</fx:Declarations>
<s:Label text="File System Paths" top="10" width="100%" textAlign="center"/>
<s:TextArea id="info" width="100%" height="100%" top="40" editable="false"/>
</s:Application>
![](http://tutorials.programming4.us/image/062013/File%20System%20Access_1.jpg)
2. Read and Write to the File System
Adobe AIR gives you 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 following code. There are two
TextArea and two Button components
that make up this sample. The first TextArea with the
ID of “contents” will hold the contents of what is to be written to the
file; and the second, with the ID of “results,” will output the file
contents when read back. The application is shown in Figure 2.
Clicking on the Button component with the label
of “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 with the name “stream” is created to
write the data to the file. The open method is called
on the stream object and the file and FileMode.WRITE is passed in, which will open
the file with write permissions. Next, the
writeUTFBytes method is called and the
contents.text is passed in. Finally, the stream is
closed.
Clicking on the Button with the label of “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 with the name “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 is 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 3 shows the contents
of the file and the path to the newly created file within the
TextArea with the ID of
results.
<?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[
protected function button1_clickHandler(event:MouseEvent):void
{
var file:File = File.applicationStorageDirectory.
resolvePath("samples/test.txt");
var stream:FileStream = new FileStream()
stream.open(file, FileMode.WRITE);
stream.writeUTFBytes(contents.text);
stream.close();
}
protected function button2_clickHandler(event:MouseEvent):void
{
var file:File = File. applicationStorageDirectory.
resolvePath("samples/test.txt");
var stream: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:Label id="path" left="10" top="160"/>
<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>
![](http://tutorials.programming4.us/image/062013/File%20System%20Access_2.jpg)
![](http://tutorials.programming4.us/image/062013/File%20System%20Access_3.jpg)