Home  >  Development  >  features

Beginning AIR - Accessing the File System Part 3

AddThis Social Bookmark Button
1_0470229047.jpg
This excerpt is the third part of Chapter 8 of the upcoming (March 17, 2008 - NOW SHIPPING) book titled Beginning AIR: Building Applications for the Adobe Integrated Runtime ISBN: 0470229047. Beginning AIR is written in a walk through style where the examples are built upon throughout each section of the book. The easiest way to learn how to use the API's covered is to create the examples and follow along as they are built upon.

Part 1 of this series gave examples of how to work with directories within the file system. Part 2 of this series gave examples of how to work the File and FileStream classes to work within the file system. This excerpt covers the differences between asynchronous and synchronous calls to the File classes methods. It will also show how to create temporary directories and files.

Asynchronous versus Synchronous

The examples given so far have concentrated on synchronous methods. Calling a method synchronously means that while the call is being made the application pauses until the method call has completed. For example, let's say that you would like to read in file information for 1,000 images that exist in an image folder. Doing this as a synchronous method would mean that the application would be unusable while it is reading in the image files. A better way to do this would be to use an asynchronous method, which would allow the user to continue to use the application while the asynchronous method executes. While the asynchronous method is executing, you can receive updates of its progress by setting up event listeners.

There are asynchronous versions of many of the File class methods. Here is the complete list:

Directories
copyToAsync()
deleteDirectoryAsync()
getDirectoryListingAsync()
moveToAsync()
moveToTrashAsync()

Files
copyToAsync()
deleteFileAsync()
moveToAsync()
moveToTrashAsync()

FileStream
openAsync()

The following examples will demonstrate how to use the deleteDirectoryAsync() method of the File class as well as the openAsync() method of the FileStream class. The same principles can be used for all of the other asynchronous methods as well.

To test the example in Listing 8-6, create a directory first by clicking on the Create Directory button, and then delete the directory asynchronously by clicking on the Delete Directory button. The results can be seen in Figure 8-6.

<?xml version="1.0" encoding="utf-8"?>
<mx:WindowedApplication xmlns:mx="http://www.adobe.com/2006/mxml"
    layout="absolute">
    <mx:Script>
    <![CDATA[
    import flash.filesystem.File;
    import mx.events.FileEvent;
    import mx.controls.Alert;
    /*
    Create a new directory
    */
    private function createDirectory():void{
         var newDirectory:File=File.desktopDirectory.resolvePath("MyNewDirectory");
         newDirectory.createDirectory();
     }
    /*
    Delete directory Asynchronously
    */
    private function deleteDirectory():void{
         var directory:File = File.desktopDirectory.resolvePath("MyNewDirectory");
         directory.addEventListener(Event.COMPLETE,completeHandler);
         directory.deleteDirectoryAsync();
     }
    private function completeHandler(event:Event):void {
         Alert.show("Directory deleted","Complete Handler");
     }
    ]]>
    </mx:Script>
    <mx:Button label="Create Directory"
        click="createDirectory()" y="10" horizontalCenter="0"/>
    <mx:Button label="Delete Directory"
        click="deleteDirectory()" y="40" horizontalCenter="0"/>
    </mx:WindowedApplication>

229040_fg806.png
Figure 8-6: The complete handler triggered when Delete event is completed.

The example in Listing 8-7 demonstrates how to use the openAsynch method of the FileStream class. To test this, create a file by clicking on the Create File button, and then read the file asynchronously by clicking on the Read File Asynchronously button. The results can be seen in Figure 8-7.

<?xml version="1.0" encoding="utf-8"?>
<mx:WindowedApplication xmlns:mx="http://www.adobe.com/2006/mxml"
    layout="absolute">
    <mx:Script>
    <![CDATA[
    import mx.controls.Alert;
    import mx.events.FileEvent;
    import flash.filesystem.FileMode;
    import flash.filesystem.FileStream;
    import flash.filesystem.File;
    private var fileContents:String;
    /*
    Create a new file
    */
    private function createFile():void{
         var newFile:File = File.desktopDirectory.resolvePath("MyNewFile.txt");
         var fileStream:FileStream = new FileStream();
         fileStream.open(newFile, FileMode.WRITE);
         fileStream.writeUTFBytes("Beginning AIR by Rich Tretola");
         fileStream.close();
     }
    /*
    Read in a file
    Note: Be sure to create the file first
    */
    private function readFileAsynch():void{
         var resourceFile:File = File.desktopDirectory.resolvePath("MyNewFile.txt");
         var fileStream:FileStream = new FileStream();
         fileStream.openAsync(resourceFile, FileMode.READ);
         fileStream.addEventListener(Event.CLOSE,completeHandler);
         fileStream.addEventListener(IOErrorEvent.IO_ERROR,errorHandler);
         fileContents = fileStream.readUTFBytes(fileStream.bytesAvailable);
         fileStream.close();
     }
    
    private function completeHandler(event:Event):void {
         Alert.show("File read completed","Complete Handler");
     }

    private function errorHandler(event:IOErrorEvent):void {
         Alert.show("Error " + event.text,"errorHandler");
     }
    ]]>
    </mx:Script>
    <mx:Button label="Create File"
        click="createFile()" y="10" horizontalCenter="0"/>
    <mx:Button label="Read File Asynchronously"
        click="readFileAsynch()" y="40" horizontalCenter="0"/>
</mx:WindowedApplication>

229040_fg807.png
Figure 8-7: The complete handler triggered when File Read is completed.

Creating Temporary Files and Directories

There may be occasions when you would like to have local file storage for temporary files and directories. AIR has included methods within the File API to create temporary directories and files. After importing the File class, a temporary (temp) directory can be created by calling the createTempDirectory() method on the File class, and a temp file can be created by calling the createTempFile() method.

Listing 8-8 shows how to create a temporary directory and a temp file.

<?xml version="1.0" encoding="utf-8"?>
<mx:WindowedApplication xmlns:mx="http://www.adobe.com/2006/mxml"
    layout="absolute">
    <mx:Script>
    <![CDATA[
    import mx.controls.Alert;
    import flash.filesystem.File;
    private function createTempDirectory():void{
         var tempDirectory:File = File.createTempDirectory();
         Alert.show("nativePath = " + tempDirectory.nativePath +
         "\n\nisDirectory = " + tempDirectory.isDirectory,
         "tempDirectory");
     }

    private function createTempFile():void{
         var tempFile:File = File.createTempFile();
         Alert.show("nativePath = " + tempFile.nativePath +
         "\n\nisDirectory = " + tempFile.isDirectory,
         "tempFile");
     }
    ]]>
    </mx:Script>
    <mx:Button label="Create Temp Directory"
        click="createTempDirectory()" horizontalCenter="0"
        y="10"/>
    <mx:Button label="Create Temp File"
        click="createTempFile()" horizontalCenter="0"
        y="40"/>
</mx:WindowedApplication>

The results can be see in Figures 8-8 and 8-9. Notice that the output of the isDirectory property in each figure shows that the temp directory is recognized as a directory, while the temp file is not. If you are running this sample on a Windows machine, your expected outcome would be a file path similar to C:\Documents and Settings\rich\Local Settings\Temp\fla28F.tmp.

229040_fg808.png
Figure 8-8: The nativePath and isDirectory property of a temp directory created on Mac OS X.

229040_fg809.png
Figure 8-9: The nativePath and isDirectory properties of a temp file created on Mac OS X.

To read the rest of this chapter, which includes the browse for files dialog, and read the rest of the book please order a copy at Amazon.com. The 1st 2 people to email me proof that they have the book in hand can get a free copy of Professional Flex 2. See this post at EverythingFlex.com for details.

Comments

Leave a comment


Type the characters you see in the picture above.

Tag Cloud

Related Books

Development Series

Get an overview of the tools and technologies that work together to allow developers to build Rich Internet Applications (RIAs) quickly and easily.

Anatomy of an Enterprise Flex RIA

Recent Comments

Archives


 
 


Or, visit our complete archive.  

About This Site

Welcome to the premiere community site for all things RIA sponsored by O'Reilly Media and Adobe Systems Incorporated.

About Us
Meet the Experts
Meet Our Contributors
Send Us Feedback