<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom" 
      xmlns:thr="http://purl.org/syndication/thread/1.0">
  <link rel="alternate" type="text/html" href="http://www.insideria.com/2008/04/beginning-air-accessing-the-fi-2.html" />
  <link rel="self" type="application/atom+xml" href="http://www.insideria.com/atom.xml" />
  <id>tag:www.insideria.com,2009://34/tag:www.insideria.com,2008://34.23170-</id>
  <updated>2009-11-16T15:48:18Z</updated>
  <title>Comments for Beginning AIR - Accessing the File System Part 3 (http://www.insideria.com/2008/04/beginning-air-accessing-the-fi-2.html)</title>
  <generator uri="http://www.sixapart.com/movabletype/">Movable Type 4.21-en</generator>
  <entry>
    <id>tag:www.insideria.com,2008://34.23170</id>
    <link rel="alternate" type="text/html" href="http://www.insideria.com/2008/04/beginning-air-accessing-the-fi-2.html" />
    <link rel="service.edit" type="application/atom+xml" href="http://blogs.oreilly.com/cgi-bin/mt/mt-atom.cgi/weblog/blog_id=34/entry_id=23170" title="Beginning AIR - Accessing the File System Part 3" />
    <published>2008-04-02T17:00:00Z</published>
    <updated>2009-04-27T16:50:37Z</updated>
    <title>Beginning AIR - Accessing the File System Part 3</title>
    <summary>This excerpt is the second 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&apos;s covered is to create the examples and follow along as they are built upon.

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.</summary>
    <author>
      <name>Rich Tretola</name>
      <uri>http://blog.everythingflex.com</uri>
    </author>
    
    <category term="Features" />
    
    <content type="html" xml:lang="en" xml:base="http://www.insideria.com/">
      <![CDATA[<div class="ap_r" style="margin: 16px;"><a href="http://www.insideria.com/upload/2008/03/beginning_air_accessing_the_fi/1_0470229047.jpg" class="highslide" onclick="return hs.expand(this)"><img src="http://www.insideria.com/upload/2008/03/beginning_air_accessing_the_fi/1_0470229047.jpg" alt="1_0470229047.jpg" title="Click to enlarge" width="148"/></a></div>This excerpt is the third part of Chapter 8 of the upcoming (March 17, 2008 - <strong>NOW SHIPPING</strong>) book titled <a href="http://www.amazon.com/gp/product/0470229047?&camp=212361&creative=383837&linkCode=wss&tag=everythingfle-20" target="_blank">Beginning AIR: Building Applications for the Adobe Integrated Runtime ISBN: 0470229047</a>. 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.
<br/>
<br/>
<a href="http://www.insideria.com/2008/03/beginning-air-accessing-the-fi.html">Part 1</a> of this series gave examples of how to work with directories within the file system. <a href="http://www.insideria.com/2008/03/beginning-air-accessing-the-fi-1.html">Part 2</a> 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.
<br/>
<br/>

<strong>Asynchronous versus Synchronous</strong><br/>
<br/>
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.<br/>
<br/>
There are asynchronous versions of many of the File class methods. Here is the complete list:
<br/>
<br/>
<strong>Directories</strong><br/>
copyToAsync()<br/>
deleteDirectoryAsync()<br/>
getDirectoryListingAsync()<br/>
moveToAsync()<br/>
moveToTrashAsync()<br/>
<br/>
<strong>Files</strong><br/>
copyToAsync()<br/>
deleteFileAsync()<br/>
moveToAsync()<br/>
moveToTrashAsync()<br/>
<br/>
<strong>FileStream</strong><br/>
openAsync()<br/>
<br/>
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.<br/>
<br/>
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.<br/>
<br/>
 <div class="acode" style="overflow: auto; padding: 10px;" ><div style="overflow-x: visible;">
<code language="perl">
<pre>
&lt;?xml <span class="category2">version</span>="<span class="quote">1.0</span>" encoding="<span class="quote">utf-8</span>"?&gt;
&lt;mx:WindowedApplication xmlns:mx="<span class="quote">http://www.adobe.com/2006/mxml</span>"
    layout="<span class="quote">absolute</span>"&gt;
    &lt;mx:Script&gt;
    &lt;![CDATA[
    <span class="category1">import</span> flash.filesystem.File;
    <span class="category1">import</span> mx.events.FileEvent;
    <span class="category1">import</span> mx.controls.Alert;
    <span class="blockcomment">/*
    Create a new directory
    */</span>
    <span class="category1">private</span> <span class="category1">function</span> createDirectory():<span class="category1">void</span>{
         <span class="category1">var</span> newDirectory:File=File.desktopDirectory.resolvePath("<span class="quote">MyNewDirectory</span>");
         newDirectory.createDirectory();
     }
    <span class="blockcomment">/*
    Delete directory Asynchronously
    */</span>
    <span class="category1">private</span> <span class="category1">function</span> deleteDirectory():<span class="category1">void</span>{
         <span class="category1">var</span> directory:File = File.desktopDirectory.resolvePath("<span class="quote">MyNewDirectory</span>");
         directory.addEventListener(Event.COMPLETE,completeHandler);
         directory.deleteDirectoryAsync();
     }
    <span class="category1">private</span> <span class="category1">function</span> completeHandler(event:Event):<span class="category1">void</span> {
         Alert.<span class="category2">show</span>("<span class="quote">Directory deleted</span>","<span class="quote">Complete Handler</span>");
     }
    ]]&gt;
    &lt;/mx:Script&gt;
    &lt;mx:<span class="category2">Button</span> <span class="category1">label</span>="<span class="quote">Create Directory</span>"
        click="<span class="quote">createDirectory()</span>" <span class="category2">y</span>="<span class="quote">10</span>" horizontalCenter="<span class="quote">0</span>"/&gt;
    &lt;mx:<span class="category2">Button</span> <span class="category1">label</span>="<span class="quote">Delete Directory</span>"
        click="<span class="quote">deleteDirectory()</span>" <span class="category2">y</span>="<span class="quote">40</span>" horizontalCenter="<span class="quote">0</span>"/&gt;
    &lt;/mx:WindowedApplication&gt;</pre>
</code>

</div></div> 
<br/>
<div class="ap_c"style="margin: 16px;"><a href="http://www.insideria.com/upload/2008/03/beginning_air_accessing_the_fi_2/229040_fg806.png" class="highslide" onclick="return hs.expand(this)"><img src="http://www.insideria.com/upload/2008/03/beginning_air_accessing_the_fi_2/229040_fg806.png" alt="229040_fg806.png" title="Click to enlarge" width="400"/></a><div class="apcaption">Figure 8-6: The complete handler triggered when Delete event is completed.</div></div>
<br/>
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.<br/>
<br/>
 <div class="acode" style="overflow: auto; padding: 10px;" ><div style="overflow-x: visible;">
<code language="perl">
<pre>
&lt;?xml <span class="category2">version</span>="<span class="quote">1.0</span>" encoding="<span class="quote">utf-8</span>"?&gt;
&lt;mx:WindowedApplication xmlns:mx="<span class="quote">http://www.adobe.com/2006/mxml</span>"
    layout="<span class="quote">absolute</span>"&gt;
    &lt;mx:Script&gt;
    &lt;![CDATA[
    <span class="category1">import</span> mx.controls.Alert;
    <span class="category1">import</span> mx.events.FileEvent;
    <span class="category1">import</span> flash.filesystem.FileMode;
    <span class="category1">import</span> flash.filesystem.FileStream;
    <span class="category1">import</span> flash.filesystem.File;
    <span class="category1">private</span> <span class="category1">var</span> fileContents:<span class="category2">String</span>;
    <span class="blockcomment">/*
    Create a new file
    */</span>
    <span class="category1">private</span> <span class="category1">function</span> createFile():<span class="category1">void</span>{
         <span class="category1">var</span> newFile:File = File.desktopDirectory.resolvePath("<span class="quote">MyNewFile.txt</span>");
         <span class="category1">var</span> fileStream:FileStream = <span class="category1">new</span> FileStream();
         fileStream.open(newFile, FileMode.WRITE);
         fileStream.writeUTFBytes("<span class="quote">Beginning AIR by Rich Tretola</span>");
         fileStream.<span class="category2">close</span>();
     }
    <span class="blockcomment">/*
    Read in a file
    Note: Be sure to create the file first
    */</span>
    <span class="category1">private</span> <span class="category1">function</span> readFileAsynch():<span class="category1">void</span>{
         <span class="category1">var</span> resourceFile:File = File.desktopDirectory.resolvePath("<span class="quote">MyNewFile.txt</span>");
         <span class="category1">var</span> fileStream:FileStream = <span class="category1">new</span> FileStream();
         fileStream.openAsync(resourceFile, FileMode.READ);
         fileStream.addEventListener(Event.CLOSE,completeHandler);
         fileStream.addEventListener(IOErrorEvent.IO_ERROR,errorHandler);
         fileContents = fileStream.readUTFBytes(fileStream.bytesAvailable);
         fileStream.<span class="category2">close</span>();
     }
    
    <span class="category1">private</span> <span class="category1">function</span> completeHandler(event:Event):<span class="category1">void</span> {
         Alert.<span class="category2">show</span>("<span class="quote">File read completed</span>","<span class="quote">Complete Handler</span>");
     }

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

</div></div> 
<br/>
<div class="ap_c"style="margin: 16px;"><a href="http://www.insideria.com/upload/2008/03/beginning_air_accessing_the_fi_2/229040_fg807.png" class="highslide" onclick="return hs.expand(this)"><img src="http://www.insideria.com/upload/2008/03/beginning_air_accessing_the_fi_2/229040_fg807.png" alt="229040_fg807.png" title="Click to enlarge" width="400"/></a><div class="apcaption">Figure 8-7: The complete handler triggered when File Read is completed.</div></div>
<br/>
<strong>Creating Temporary Files and Directories</strong><br/>
<br/>
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.<br/>
<br/>
Listing 8-8 shows how to create a temporary directory and a temp file.<br/>
<br/>
 <div class="acode" style="overflow: auto; padding: 10px;" ><div style="overflow-x: visible;">
<code language="perl">
<pre>
&lt;?xml <span class="category2">version</span>="<span class="quote">1.0</span>" encoding="<span class="quote">utf-8</span>"?&gt;
&lt;mx:WindowedApplication xmlns:mx="<span class="quote">http://www.adobe.com/2006/mxml</span>"
    layout="<span class="quote">absolute</span>"&gt;
    &lt;mx:Script&gt;
    &lt;![CDATA[
    <span class="category1">import</span> mx.controls.Alert;
    <span class="category1">import</span> flash.filesystem.File;
    <span class="category1">private</span> <span class="category1">function</span> createTempDirectory():<span class="category1">void</span>{
         <span class="category1">var</span> tempDirectory:File = File.createTempDirectory();
         Alert.<span class="category2">show</span>("<span class="quote">nativePath = </span>" + tempDirectory.nativePath +
         "<span class="quote">\n\nisDirectory = </span>" + tempDirectory.isDirectory,
         "<span class="quote">tempDirectory</span>");
     }

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

</div></div> 
<br/>
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.<br/>
<br/>
<div class="ap_c"style="margin: 16px;"><a href="http://www.insideria.com/upload/2008/03/beginning_air_accessing_the_fi_2/229040_fg808.png" class="highslide" onclick="return hs.expand(this)"><img src="http://www.insideria.com/upload/2008/03/beginning_air_accessing_the_fi_2/229040_fg808.png" alt="229040_fg808.png" title="Click to enlarge" width="400"/></a><div class="apcaption">Figure 8-8: The nativePath and isDirectory property
of a temp directory created on Mac OS X.</div></div>
<br/>
<div class="ap_c"style="margin: 16px;"><a href="http://www.insideria.com/upload/2008/03/beginning_air_accessing_the_fi_2/229040_fg809.png" class="highslide" onclick="return hs.expand(this)"><img src="http://www.insideria.com/upload/2008/03/beginning_air_accessing_the_fi_2/229040_fg809.png" alt="229040_fg809.png" title="Click to enlarge" width="400"/></a><div class="apcaption">Figure 8-9: The nativePath and isDirectory properties
of a temp file created on Mac OS X.</div></div>
<br/>
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 <a href="http://www.amazon.com/gp/product/0470229047?&camp=212361&creative=383837&linkCode=wss&tag=everythingfle-20" target="_blank">Amazon.com</a>. 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 <a href="http://blog.everythingflex.com/2008/03/19/get-a-free-flex-book/" target="_blank">this post at EverythingFlex.com</a> for details.



]]>
      
    </content>
  </entry>

  <entry>
    <id>tag:www.insideria.com,2008://34.23170-comment:2048020</id>
    <thr:in-reply-to ref="tag:www.insideria.com,2008://34.23170" type="text/html" href="http://www.insideria.com/2008/04/beginning-air-accessing-the-fi-2.html"/>
    <link rel="alternate" type="text/html" href="http://www.insideria.com/2008/04/beginning-air-accessing-the-fi-2.html#comment-2048020" />
    <title>Comment from Jed Schneider on 2008-12-03</title>
    <author>
        <name>Jed Schneider</name>
        <uri>http://www.jedschneider.net</uri>
    </author>
    <content type="html" xml:lang="en" xml:base="http://www.jedschneider.net">
        <![CDATA[<p>Bought your book a couple of months ago. A great place to start learning AIR. Well reccomended to beginners. If you already have some Flex understanding, it might be a bit over-simplistic but a great reference for examples using the AIR classes. Like the above example, clear coding, and to the point. Thanks for all your hard work.</p>]]>
    </content>
    <published>2008-12-03T14:49:54Z</published>
  </entry>

  <entry>
    <id>tag:www.insideria.com,2008://34.23170-comment:2048022</id>
    <thr:in-reply-to ref="tag:www.insideria.com,2008://34.23170" type="text/html" href="http://www.insideria.com/2008/04/beginning-air-accessing-the-fi-2.html"/>
    <link rel="alternate" type="text/html" href="http://www.insideria.com/2008/04/beginning-air-accessing-the-fi-2.html#comment-2048022" />
    <title>Comment from Rich Tretola on 2008-12-03</title>
    <author>
        <name>Rich Tretola</name>
        <uri>http://www.insideria.com</uri>
    </author>
    <content type="html" xml:lang="en" xml:base="http://www.insideria.com">
        <![CDATA[<p>Thanks Jed,</p>

<p>I would also recommend the new AIR cookbook :-)</p>

<p><a href="http://www.amazon.com/Adobe-AIR-Cookbook-Application-Developers/dp/0596522509">http://www.amazon.com/Adobe-AIR-Cookbook-Application-Developers/dp/0596522509</a></p>]]>
    </content>
    <published>2008-12-03T15:01:02Z</published>
  </entry>

  <entry>
    <id>tag:www.insideria.com,2008://34.23170-comment:2059183</id>
    <thr:in-reply-to ref="tag:www.insideria.com,2008://34.23170" type="text/html" href="http://www.insideria.com/2008/04/beginning-air-accessing-the-fi-2.html"/>
    <link rel="alternate" type="text/html" href="http://www.insideria.com/2008/04/beginning-air-accessing-the-fi-2.html#comment-2059183" />
    <title>Comment from kanu kukreja on 2009-05-09</title>
    <author>
        <name>kanu kukreja</name>
        <uri></uri>
    </author>
    <content type="html" xml:lang="en" xml:base="">
        <![CDATA[<p>How we can create file at user specific path?</p>

<p>var file:File=File.userDirectory.resolvePath(”c:\Data\Hello.txt”);. <br />
I got this error “Error #3003: File or directory does not exist.”</p>]]>
    </content>
    <published>2009-05-09T11:09:02Z</published>
  </entry>

  <entry>
    <id>tag:www.insideria.com,2008://34.23170-comment:2069329</id>
    <thr:in-reply-to ref="tag:www.insideria.com,2008://34.23170" type="text/html" href="http://www.insideria.com/2008/04/beginning-air-accessing-the-fi-2.html"/>
    <link rel="alternate" type="text/html" href="http://www.insideria.com/2008/04/beginning-air-accessing-the-fi-2.html#comment-2069329" />
    <title>Comment from Andrew Westberg on 2009-07-28</title>
    <author>
        <name>Andrew Westberg</name>
        <uri>http://www.nitrolm.com/blog</uri>
    </author>
    <content type="html" xml:lang="en" xml:base="http://www.nitrolm.com/blog">
        <![CDATA[<p>I was using some of this openAsync code for files as a basis for my own stuff.  I think there's a problem loading large files with how you have it coded.  Your code assumes that the entire contents of the file are ready to be read in immediately after calling openAsync.</p>

<p>In Listing 8-7, if the file is large enough, fileStream.bytesAvailable won't be the complete size of the file so not everything will get read into fileContents.</p>

<p>To get around this, you should instead set up a ProgressEvent.PROGRESS handler function and continually read in the contents of the fileStream as it loads in chunks.</p>

<p>Finally, in that handler, you should call the fileStream.close() method only when event.bytesLoaded == event.bytesTotal.  This way, your close method will only get called with the complete fileContents after everything has been loaded.</p>

<p>The example code is only working currently because the file you're writing is quite tiny.  In my own code, reading in a 46kb file came down in two chunks.  ~40kb and ~6kb.  If I used your code directly, I only had the 40kb in fileContents.</p>]]>
    </content>
    <published>2009-07-28T17:57:29Z</published>
  </entry>

  <entry>
    <id>tag:www.insideria.com,2008://34.23170-comment:2070383</id>
    <thr:in-reply-to ref="tag:www.insideria.com,2008://34.23170" type="text/html" href="http://www.insideria.com/2008/04/beginning-air-accessing-the-fi-2.html"/>
    <link rel="alternate" type="text/html" href="http://www.insideria.com/2008/04/beginning-air-accessing-the-fi-2.html#comment-2070383" />
    <title>Comment from Patrick on 2009-08-14</title>
    <author>
        <name>Patrick</name>
        <uri></uri>
    </author>
    <content type="html" xml:lang="en" xml:base="">
        <![CDATA[<p>@kanu kukreja</p>

<p>var file:File = new File(”c:\Data\Hello.txt”);</p>

<p>When using File.userDirectory you can't go outside that folder</p>]]>
    </content>
    <published>2009-08-14T13:59:23Z</published>
  </entry>

  <entry>
    <id>tag:www.insideria.com,2008://34.23170-comment:2103931</id>
    <thr:in-reply-to ref="tag:www.insideria.com,2008://34.23170" type="text/html" href="http://www.insideria.com/2008/04/beginning-air-accessing-the-fi-2.html"/>
    <link rel="alternate" type="text/html" href="http://www.insideria.com/2008/04/beginning-air-accessing-the-fi-2.html#comment-2103931" />
    <title>Comment from gochi on 2009-09-18</title>
    <author>
        <name>gochi</name>
        <uri>http://www.goji-unveiled.com/gochi.php</uri>
    </author>
    <content type="html" xml:lang="en" xml:base="http://www.goji-unveiled.com/gochi.php">
        <![CDATA[<p>The first two parts would be really great and helpful for me because I need to see and learn how to work with directories within the file system and also how to work the File and FileStream classes to work within the file system.And for this reason those two parts would be very handy to me.</p>]]>
    </content>
    <published>2009-09-18T11:52:05Z</published>
  </entry>

  <entry>
    <id>tag:www.insideria.com,2008://34.23170-comment:2105056</id>
    <thr:in-reply-to ref="tag:www.insideria.com,2008://34.23170" type="text/html" href="http://www.insideria.com/2008/04/beginning-air-accessing-the-fi-2.html"/>
    <link rel="alternate" type="text/html" href="http://www.insideria.com/2008/04/beginning-air-accessing-the-fi-2.html#comment-2105056" />
    <title>Comment from perfume on 2009-09-19</title>
    <author>
        <name>perfume</name>
        <uri>http://www.perfumewomens.com</uri>
    </author>
    <content type="html" xml:lang="en" xml:base="http://www.perfumewomens.com">
        <![CDATA[<p>Superb article.I really enjoyed reading the whole of it.For me creating the temporary file and directory portion has been really handy as I was also having some trouble with this issue,but now it seems over.Thanks for sharing this very handy tutorial.</p>]]>
    </content>
    <published>2009-09-19T07:53:25Z</published>
  </entry>

</feed
